Click here to Skip to main content
14,934,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys in new here and i started to learn programming 2 weeks ago and i was working on some project form book Learn programming with C++ book and looks like some of the code in that book is outdated(Tryed to find on the internet solution but everything looks the same ... ). So i have problem with for loops. When i execute program it show 1 message and user need to answer with yes and after that it show message 2 but program doesnt wait user to enter "value" instead program execute next message from FOR loop and end the program(That message should be executed only if student number was invalid ).

How program looks like when executed:
Do you want to calculate the grade ? : YES
 Enter student type " << "(1-English, 2-Math,3-Sience):Please enter valid number...
Process returned 1 <0x1> execution time ...

What I have tried:

cout << "Do you want to callculate the grade ? : ";
    cin >> gradestocalculater;

    for (int i = 0; i < gradestocalculater.length();i++){
        gradestocalculater[i] = toupper(gradestocalculater[i]); //WORKING COMMAND 

    while(gradestocalculater == "YES")

    cout << "Enter student type " << "(1-English, 2-Math,3-Sience): "; // COMAND EXECUTED
    cin.getline(response,256); //COMAND DONT WORKING ???

    if (strlen(response) == 0){
        cout << "Please enter valid number...";
        return 1;} 
Updated 23-Jul-18 1:41am

That is because you are using two methods to read the input:
the operator>> (istream) - C++ Reference[^] (also called extraction operator) and istream::getline - C++ Reference[^].

The first will read strings until a white space character occurs (which includes the \n new line when pressing Enter) without storing and reading that white space character while getline() reads upto and including the new line character storing it not in the supplied buffer.

So the first will read the input (assuming "YES") but the new line character which stops the reading is still in the input buffer. When calling getline() afterwards that will initially read the new line character and return an empty buffer.

The common solution to this problem is to avoid mixing these two input methods. In most cases using getline() is the better method because it allows to enter strings containing spaces.
Laske 23-Jul-18 8:27am
Damn your good :D , thanks man really appreciate it.
Jochen Arndt 23-Jul-18 8:30am
You are welcome and thank you for the feedback and accepting my solution.
Laske 23-Jul-18 15:59pm
Is there any way that i can do this with floats and integers?
cin >> float; doesnt work and also getline(cin,float) doesnt work.
Jochen Arndt 23-Jul-18 17:52pm
std::cin >> var works also for bool, int, float, and double.

If you have a string use atof() or strtod() to convert it to a double, atoi() to convert to an int or strtol() / strtoul() to convert to a long / unsigned long.
Laske 24-Jul-18 4:37am
thanks again :)
First aid:
cout << "Response: " << response;

The response is read, but length is greater than zero and you are processing the code afterwards, but not as you wanted.

Use the debugger to see what is going on.

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