#include <iostream>
#include <string>
using namespace std;
int command;
string version;
string username;
int main()
{
username = "Default";
version = "Alpha 1.0";
cout << "Welcome to COS Version" << " " << version << endl;
cout << "This OS is based in complete command line" << endl;
cout << "If you need help just type in help to get a list of commands" << endl;
cout << "Type in CreateUser to create a user" << endl;
goto command;
command:
cin >> command; // <---- BAD CIN (this isnt here in the real code)
if (command = 'CreateUser'){
if (username != username){
cout << "It looks like there is already an account on here" << endl;
}
else if (username == username){
cout << "Username?" << endl;
cin >> username;
cout << "Welcome " << username << endl;
goto command;
}
}
}
Here is the code i am using, its just a stupid simple command line OS but "cin >> command is being a pain in the ass and looping and acting like it isnt there so it is looping until it crashes. Also im at school so i only have access to online c++ compilers and we have chromebooks. But long story short, how do i get my cin to stop looping
But long story short, how do i get my cin to stop looping
Start by using a "real" loop instead of the goto. The goto is considered a very very bad practice, learn to use one of the more acceptable types of loops like a while{}, do{}while(), or for(;;).
Also have both a variable and a label that have the same name is a very bad idea.
Do you know the difference between a "string" constant and a 'character' constant?
Why all the global variables? You have one function so put your variable declarations inside that function.
When is that ever going to be true? Comparing a variable not equal to itself will always be false.
Line 23:
elseif (username == username)
There is no need for the second if when the condition is the inverse of the first if (ignoring the fact that the second if will always be true). That's what else means.
Line 5: command is an int. So your cin at line 18 is expecting an int. If you actually try to enter "CreateUser", cin will stop at the first non-numeric ("C"), leaving the entire entry in the input buffer and it will still be there when you loop back for the next cin.
Line 10: You can't create a 10 character character literal. This shouldn't compile.
Line 10: You're using the assignment operator (=), not the comparison operator (==).
As jlb said, get rid of the gotos. Very bad practice.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Goto is my choice in this example because i need it to go back to that command every time another function is executed so lets say someone creates a user. after that i want them to be able to go back and type in any command they want, simulating a command line and i dont see any way to loop that with a while command.
I get that the "(username == username)" is a mistake i need to declare a different variable that is set to a default value and then compare it to the new username. I will also fix the variables.
But you in no way helped me solve my problem which is quite disappointing ◔_◔.
Read and understand what I said about command being an int.
If you're trying to enter "CreateUser", cin is going to IGNORE your input and just leave "CreateUser" in the input buffer which it will again ignore on the next cin of command.
If you want to enter strings, change command to a string.
i don't see any way to loop that with a while command.
Here's a while loop with no gotos (but with existing mistakes).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while (true)
{ cin >> command; // <---- BAD CIN (this isnt here in the real code)
if (command = 'CreateUser')
{ if (username != username)
{ cout << "It looks like there is already an account on here" << endl;
}
elseif (username == username)
{ cout << "Username?" << endl;
cin >> username;
cout << "Welcome " << username << endl;
}
continue; // return to top of loop
}
}
So what should i put those variables as? string or char? If both of these are incorrect forgive me i am obviously very bad. also how would i make a loop that would perform as i stated above
i need it to go back to that command every time another function is executed so lets say someone creates a user. after that i want them to be able to go back and type in any command they want, simulating a command line
#include <iostream>
#include <string>
usingnamespace std;
int main()
{ string command; // command should be a string
string version = "Alpha 1.0";
string username = "Defaults";
cout << "Welcome to COS Version" << " " << version << endl;
cout << "This OS is based in complete command line" << endl;
cout << "If you need help just type in help to get a list of commands" << endl;
cout << "Type in CreateUser to create a user" << endl;
while (true)
{ cin >> command;
if (command == "CreateUser") // comparison operator and string literal
{ cout << "Username?" << endl;
cin >> username;
// Place check for duplicate usernames here
cout << "Welcome " << username << endl;
continue; // return to top of loop
}
}
}
And btw i wasnt talking to you when i said you in no way helped my problem i was talking to jib.
Why because I didn't give you the whole solution to your problem? You never answered any of the questions I posed in my first post so why should I bother?