loops - C++ Can't input with keyboard -


if "password" not inputted, code asks name of user. if name found, assigns first name user.

the code blow simplified version of larger 1 problem same. in else if statement, std::cout << "hello " << firstname << std::endl; repeats infinitely. if remove std::cout << "hello " << firstname << std::endl; incapable of inputting anything. it's if keyboard unplugged. tried using goto, cases, , making function out of it, yet problem persists. ideas?

#include <fstream> #include <iostream> #include <string>  int main()  { std::string user; std::string text; std::string password; std::cin >> password;     if(password == "password")     {         user = "ryan";     }     else if (password != "password")     {         std::string line;         std::string lastname;         std::string firstname;         std::string attemptuser;         std::ifstream namefile;         int offset;           std::cout << "name?" << std::endl;          namefile.open("c:\\filepath\\names.txt");          if(namefile.is_open())         {             while(!namefile.eof())             {                 while(std::cin >> firstname >> lastname)//<---needing press ctrl-z still problem!                 attemptuser = firstname + ' ' + lastname;                 if((offset = line.find(attemptuser, 0)) != std::string::npos)                  {                     namefile.close();                     std::cout << "hello " << firstname << std::endl;                     user = firstname;                 }             }         }     }         {         std::getline(std::cin, text);         if(text.find("the code") != std::string::npos)         {             std::cout << "yes " << user << ", code" << std::endl;         }     } while (text != "close"); system("pause"); return 0; } 

okay, there several problems here, learning , fixing, so:

problem zero put

using namespace std; 

before main, , stop putting std:: in front of in std:: namespace. not functional difference, should know means @ point.

problem, first:

         while(std::cin >> firstname >> lastname)//<---needing press ctrl-z still problem!             attemptuser = firstname + ' ' + lastname; 

this repeat forever unless force quit program (ctrl+c, ctrl+z, etc.)you might have meant include curly brace after while(...). regardless of follows, these 2 lines form complete block, means line

attemptuser = firstname + ' ' + lastname; 

will executed on , on until condition

std:cin >> firstname >> lastname; 

evaluates false, not happen. @ cin in c++ spec.

what may have meant this:

         while(!(std::cin >> firstname >> lastname))//<---needing press ctrl-z still problem!             attemptuser = firstname + ' ' + lastname; 

the difference subtle, yet important. also, using strings boolean operand silly.

problem, 2nd

you never read file. it's opened, checked eof, , (in theory, though never happen in implementation) closed, never read file. means ::eof always false, assuming file exists , not empty.

problem, 3rd

if(password == "password") {     ...  } else if (password != "password") {     .... } 

okay, saying

if(i alive) {     call me! } else if (i not alive) {     send flowers or raid closets; } 

skip () in else. it's redundant, among (many) other things.

problem, 4th

the block

        while(!namefile.eof())         {             while(std::cin >> firstname >> lastname)//<---needing press ctrl-z still problem!             attemptuser = firstname + ' ' + lastname;             if((offset = line.find(attemptuser, 0)) != std::string::npos)              {                 namefile.close();                 std::cout << "hello " << firstname << std::endl;                 user = firstname;             }         } 

will utterly fail in epic fashion if offset ever equals line.find(attemptuser,0), since close file, check eof on next loop, happen unless have figured out how input nothing via keyboard. can difficult.

other

if using visual studio, enter ctrl+a, ctrl+kd. should reformat code (depending on key bindings), , show interesting things block/indentation structure might surprise you.

guessing

i'm going go out on limb, , this:

        while(!namefile.eof())         {             while(std::cin >> firstname >> lastname)//<---needing press ctrl-z still problem!             attemptuser = firstname + ' ' + lastname;             if((offset = line.find(attemptuser, 0)) != std::string::npos)              {                 namefile.close();                 std::cout << "hello " << firstname << std::endl;                 user = firstname;             }         } 

should this:

std::cin >> firstname >> lastname;  if (firstname != "" || lastname != "") {     attemptuser = firstname + " " + lastname;     while(!namefile.eof())     {         getline(namefile,line); //<< !!!!!!!!!!!!!!! read file         if (line.find(attemptuser,0) != string::npos)         {             /// found             cout << "hello " << firstname << endl;             user = firstname; /// hope don't have multiple users same first name.             break; // <------- freaking important         }     }     cout << "who you?\n"; } 

i keep going. fix stuff, realize vs2012 express does, actually, have debugger, , come more questions.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -