Having a problem. I can`t compile "++count;"

Hello.
I am just getting in to this language, and in the context of that I have started my biggest project to date.

Iam making a program where you can enter the rates which you think different games deserve at different areas. Everything is looking ok until I start compiling. My compiler says that there are something wrong with "++count;". I hope someone can help me. ;D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>

using std::cin;                  using std::setprecision;
using std::cout;                 using std::string;
using std::endl;                 using std::streamsize;         


int main(int argc, char** argv)
{
    
    std::string game;
    std::cout << "Name of the videogame: \n" << std::endl; 
    std::getline(std::cin,game); 
    std::cout << "Enter your ratings for the game " <<  game << std::endl; 
    
    
    cout << "Gameplay: ";
    double gameplay;
    cin >> gameplay;
    
       
    cout << "Graphics: ";
    double graphics;
    cin >> graphics;

        
    cout << "Sound: ";
    double sound;
    cin >> sound;

        
    cout << "Lasting Appeal: ";
    double lasting;
    cin >> lasting;
    
          double sum = 0.0;
    double x;
    

    while (x) {
int count =0;
      ++count;
        sum += x;
        
    }
// write the result
    streamsize prec = cout.precision();
    cout << "Rates of the game: " << setprecision(3)
         << lasting + 0.15 * sound + 0.18 * graphics + 0.25 * gameplay + 0.42 * sum
         << setprecision(prec) << endl;
 
     return 0;
}


I am grateful for any help.

Edit: Updated codebox
Last edited on
You never declared count nor using it anywhere but in line 44, Do you really need it?

What is line 54?
Yeah looks like you need to declare count as a variable (int count =0) then ++count will add one to it. HTH.
If I remove the "++count;"- line the program wont complete. It just freezes after I have entered all the rates, without showing the final result. Same happens if I remove line 54.

rej3kt: The program compiled as it should after I put in the int "int count =0", even though it wont write the final result
Last edited on
That is because the condition of the loop will never be false, it is an uninitialized variable which never gets a vaule
As bazzy stated the variable x is unintialized, meaning that it will contain some garbage value, and as long as that value is something other than zero the loop will run. Since the value of x isn't changed you'll end up with an infinite loop. Another problem with this while loop is that every time it runs the value for the variable count is re-intialized to zero again. Meaning that ++count isn't ever being incremented past one and the variable count is pointlessly being toggled back and forth from zero to one and back to zero over and over again. Placing the declaration and intializtion of the variable count prior to the while loop will solve the latter problem.
That is because the condition of the loop will never be false, it is an uninitialized variable which never gets a value...is it right
[url=http://www.DigBands.com/store]DigBands[/url]
butterz: How do I initialize the value "x", and what do I have to do in order to solve the problem with the X-value?

1
2
3
4
5
6
7
    double sum = 0;
    double x;
    
        int count =0; 
        ++count;
        while (x) {
        sum += x;


Will this solve my problem with the values being toggled back to zero?

Thanks for all help.
You still don't need count at all.
To initialize x add = some_value; to the declaration.
What is that loop supposed to do?
The count function is adding 1+ for every entry. Where should I instert = some_value;?



Edit:

I solved my problem thanks to Bazzy! :D

1
2
3
4
5
6
double sum = 0;
    double x;
    
      
        while (x = 0) {
        sum += x;
Last edited on
Topic archived. No new replies allowed.