Endless do while loop in C++ code? -
this question has answer here:
- using scanf() function 4 answers
char input[256]; do{ cout<<"..."; //prompt scanf("%s",&input5); //user inputs string if(strcmp(input5,"block")==0) {} //if user types block, finishes loop , goes cout @ bottom else if(strcmp(input5,"attack")==0) { cout<<"..."<<endl; } else if(strcmp(input5,"look")==0) { cout<<"..." } else { cout<<"..."<<endl; } }while(strcmp(input5,"block")!=0); //loop ends when block typed cout<<"...";
i having issues while loop. doing project school involves text adventure kind of game. user prompting how respond attack. desired command "block", move user on next sequence. when "block" typed scanf, endlessly loops , prints in "else" condition. don't see problem , feedback appreciated.
i tried code , works fine (though removed & in scanf), , created 'input5' char array.
though aside, there's few things might want change. i'd stick using 'cin' instead of scanf, you're mixing c , c++. allow use 'string' 'input5', , compare them using '==' operator, quite bit cleaner. maybe think of more descriptive name 'input5' too, if you've got lots of 'inputx' variables things messy.
edit: i'd refrain "using namespace std;", might end naming collisions.
Comments
Post a Comment