Error in <cstdio>

Hey guys,
I'm having some trouble getting this code to compile.
In fact, it starts compiling, but never stops. When I stop it manually it opens <cstdio>
With some error that it nested too deeply and a lot of files that haven't been declared.

Anyone know the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int counter;

    cout << "Enter any number! ";
    double number;
    while(cin >> number && number != 5)
        cout << "Do it again! ";
        while(getchar() = "\n")
            counter++;
                if(counter == 10) cout << "Wow, you're more patient that me. You Win!" << endl;
                break;



    if(number == 5) cout << "Dammit! you weren't suppose to hit that number!" << endl;

    return 0;
}
I see a few errors: You are assigning 'getchar' to a string literal, try comparing it with a char instead (line 12). You haven't but braces around your while or if statements, so the break statement isn't in a while loop. Finally, 'counter' isn't initialized - it will be a garbage value. Try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int counter = 0;

    cout << "Enter any number! ";
    double number;
    while(cin >> number && number != 5) {
        cout << "Do it again! ";
        while(getchar() == '\n') {
            counter++;
            if(counter == 10) {
                cout << "Wow, you're more patient that me. You Win!" << endl;
                break;
            }
        }
    }

    if(number == 5) cout << "Dammit! you weren't suppose to hit that number!" << endl;

    return 0;
}
I dont see any reason that should happen but I see a few problems in the code.

The "{" and "}" are missing for both the while loops

while(getchar() = "\n") should be while(getchar() == '\n')
There are two problems in this first getchar returns a character not a character string second you need to use the equality operator and not assignment operator
Thanks for the input, fixed up the code, however still having the problem with the compiler.

It's the same compiler errors as before. The only thing I see, is it must be in the library?
Show error text.
Could you post the exact compiler errors you are getting, please.
It's kinda hard, writing the errors, because there are a lot, like really much.

The strange thing is, I just reinstalled and now it works. However, i've tried installing before without luck.
And, the error will come back eventually :S ?
Ok, if you have a fix for now, that's good. Are you saying from previous experience that you know the error will come back? If that's the case then something is going wrong, such as some of the files being corrupted. Or perhaps less dramatically, the configuration gets changed so that the path to some of the files is incorrect.
I think you're right about the last on, it's properly because the configuration gets changed of some reason.
I'm gonna pay ekstra attention to whatever I'm doing in codeblocks from now to identify the problem.

Thanks for the help so far.
Juust another thing :)

Why is that break statement not breaking out?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int counter = 0;

    cout << "Enter any number! ";
    double number;
    while(cin >> number && number != 5) {
        cout << "Do it again! ";
        while(getchar() == '\n') {
            counter++;
            if(counter == 10)
                cout << "\nWow, you're more patient that me. You Win!" << endl;
                break;
        }
    }
    cout << "What? you're not suppose to do that!" << endl;

    return 0;
}
Is that supposed to be part of the if statement? If yes - you need braces around multiple statements.

1
2
3
4
5
if(counter == 10)
{
                cout << "\nWow, you're more patient that me. You Win!" << endl;
                break;
}

Last edited on
It does'nt help with the braces. Actually the braces do so that the line cout << "Do it again! :"; only comes every second time I hit \n
You probably want to move the if statement into the outer while loop, like this:
10
11
12
13
14
15
16
17
18
19
    while(cin >> number && number != 5) {
        cout << "Do it again! ";
        while(getchar() == '\n')
            counter++;

        if(counter == 10) {
            cout << "\nWow, you're more patient that me. You Win!" << endl;
            break;
        }
    }
Nope, that did'nt work either. Any other suggestions, guys?
Any other suggestions, guys?
If you tell exactly what do you want to achieve, we will be able to help you.
My bad, thought I had that down.

I want the user to guess a number. In this case "5". If he hits 5 it cout "you werent...." and quit the program.

Then by the 10th iteration the program should cout - "wow, you're more . . ". and quit the program.

Here's the code as of now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int counter = 0;

    cout << "Enter any number! ";
    double number;
    while(cin >> number && number != 5)
    {

        cout << "Do it again! ";
        while(getchar() == '\n')
            counter++;
            if(counter == 10){
            cout << "\nWow, you're more patient that me. You Win!" << endl;
            break;
            }
    }
    cout << "What? you're not suppose to do that!" << endl;

    return 0;
}


I'm thinking about rewriting to a switch?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    int counter = 0;
    std::cout << "Enter any number! ";
    double number;
    while(std::cin >> number && number != 5)  {
        if(++counter == 10) {
            std::cout << "Wow, you're more patient that me. You Win!\n";
            return 0;
        }
        std::cout << "Do it again!\n";
    }
    std::cout << "What? you're not suppose to do that!" << std::endl;
    return 0;
}
Wow, works like a charm, mind explaining this:

if(++counter == 10) ?
++counter == 10 means increment counter and compare it to 10 and is equivalent to
1
2
++counter;
counter == 10;
Awesome, thanks
Topic archived. No new replies allowed.