Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, so i was in visual studio for testing... and turns out i was trying to find away to put only command line/getline in and no other key, basically saying the getline and no other key as command line,

What I have tried:

here is my code





    string str;
    while (1) {
        std::getline(std::cin, str);
        if (str == "") {
            std::cout << "wrong input, try again" << std::endl;
            break;
        }
    }
    return 0;
}
Posted
Updated 4-Dec-22 22:05pm
Comments
CPallini 5-Dec-22 3:32am    
Sorry?
Could you please elaborate?
DF.EX 5-Dec-22 3:41am    
im saying, im trying to make a console app in visual studios, and there is a code in c++ console called getline where u can type a line in console app, ther eanother code called cout where u can put a line in the console not typing, and while using {} in getline u can add a code with the getline typing code, im looking for a solution where i can type the getline code command that the console app tells me to type, not any other random key, make sense?
DF.EX 5-Dec-22 3:55am    
also the string called str is for me to type any input, if i type a input in console app i would get an error saying "wrong input, try again!"
Richard MacCutchan 5-Dec-22 4:00am    
What actually happens when you run the code? As far as I can see, nothing useful if the entered string is not empty.
DF.EX 5-Dec-22 4:02am    
what happens is u type a key in and ur suppost to get the word "wrong input, try again" not the getline code where u type in the code for acommand ur something

1 solution

Quote:
im saying, im trying to make a console app in visual studios, and there is a code in c++ console called getline where u can type a line in console app, ther eanother code called cout where u can put a line in the console not typing, and while using {} in getline u can add a code with the getline typing code, im looking for a solution where i can type the getline code command that the console app tells me to type, not any other random key, make sense?
Not really, but it seems that you want to prompt the user for an input, then call getline and restrict what he can enter so it it valid data for your application.

If so, there is no "automatic" way to do that: the system has no idea what is and isn't valid as far as your application goes and there is no way to tell it.
Instead, you read the line of data, validate it according to your rules and either report a problem and let the user try again, or continue if the data is OK.
 
Share this answer
 
Comments
CPallini 5-Dec-22 4:09am    
5.At least you were able to understand something about it.
OriginalGriff 5-Dec-22 4:16am    
I have a degree in gibberish. Just ask Herself. :D
DF.EX 5-Dec-22 4:39am    
this is what i mean, expect u replace "name" with input

std::string name;

while(1){
std::getline(std::cin, name);
std::cout << "Wrong Input " << name << ", Try again!.\n";
}
OriginalGriff 5-Dec-22 5:19am    
No, you are going to have to use more words - remember, we can't see your screen, access your HDD or read your mind - all we get to work with is exactly what you type!
DF.EX 5-Dec-22 5:27am    
ik, have u tried using it in console app?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900