Search code examples
c++if-statementnestedcounterglobal

C++ incrementing global counter variables in if-else statements


so my overall goal is to make a program that would display the color associated with the wavelength number inputted by the user (400 to 445 is violet, etc.). The input needs to be between 400 and 700, and let the user know if the input is to high or to low. I also need to implement a global counter variable that will count out and display the amount of comparisons performed between the users input and the range of colors (for every else statements it goes through, increment 1). Here's what I have so far. CODE:

#include <iostream>
using namespace std;
int g_counter;

int main()
{
g_counter = 0;
int userinput;

cout<< "please enter wavelength between 400 and 700: "<< endl;
cin>>userinput;
++g_counter;
if (userinput < 400){
    cout << "Wavelength to small, try again"<< endl;
    
    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}
++g_counter;
else (userinput >= 400 && userinput <=445){
    cout<<  "Your wavelength corresponds to the color violet"<< endl;

    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}
++g_counter;
else (userinput > 445 && userinput <= 475){
    cout<< "Your wavelength corresponds to the color blue"<< endl;
    
    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}
++g_counter;
else (userinput > 475 && userinput <= 510){
    cout<< "Your wavelength corresponds to the color green"<< endl;
    
    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}
++g_counter;
else (userinput > 510 && userinput <= 570){
    cout<< "Your wavelength corresponds to the color Yellow"<< endl;

    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}
++g_counter;
else (userinput > 570 && userinput <= 590){
    cout<< "Your wavelength corresponds to the color Orange"<< endl;
    
    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}
++g_counter;
else ( userinput > 590 && userinput <= 700){
    cout<< "Your wavelength corresponds to the color red"<< endl;
    
    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}
++g_counter;
else ( userinput > 700 ){
    cout<< "wavelength to large, try again"<< endl;

    cout << "You evauluated this many conditions: "<< g_counter << endl; 
}

return 0;
}

My issue is I can't make sense of the errors I get when I compile it. ERRORS:

Homework2.cpp: In function ‘int main()’:
Homework2.cpp:19:1: error: ‘else’ without a previous ‘if’
 else (num >= 400 && num <=445){
 ^
Homework2.cpp:19:31: error: expected ‘;’ before ‘{’ token
 else (num >= 400 && num <=445){
                               ^
Homework2.cpp:26:1: error: ‘else’ without a previous ‘if’
 else (num > 445 && num <= 475){
 ^
Homework2.cpp:26:31: error: expected ‘;’ before ‘{’ token
 else (num > 445 && num <= 475){
                               ^
Homework2.cpp:32:1: error: ‘else’ without a previous ‘if’
 else (num > 475 && num <= 510){
 ^
Homework2.cpp:32:31: error: expected ‘;’ before ‘{’ token
 else (num > 475 && num <= 510){
                               ^
Homework2.cpp:38:1: error: ‘else’ without a previous ‘if’
 else (num > 510 && num <= 570){
 ^
Homework2.cpp:38:31: error: expected ‘;’ before ‘{’ token
 else (num > 510 && num <= 570){
                               ^
Homework2.cpp:44:1: error: ‘else’ without a previous ‘if’
 else (num > 570 && num <= 590){
 ^
Homework2.cpp:44:31: error: expected ‘;’ before ‘{’ token
 else (num > 570 && num <= 590){
                               ^
Homework2.cpp:50:1: error: ‘else’ without a previous ‘if’
 else ( num > 590 && num <= 700){
 ^
Homework2.cpp:50:32: error: expected ‘;’ before ‘{’ token
 else ( num > 590 && num <= 700){
                                ^
Homework2.cpp:56:1: error: ‘else’ without a previous ‘if’
 else ( num > 700 ){
 ^
Homework2.cpp:56:19: error: expected ‘;’ before ‘{’ token
 else ( num > 700 ){
                   ^

for example, the error "Homework2.cpp:19:1: error: ‘else’ without a previous ‘if’" doesn't make sense to me because there is a previous if. Also, the global counter (when the program worked before I mucked it up)kept giving the answer of 1 even if it actually checked more comparisons. Any pointers on the issues would be fantastic.


Solution

  • The else command must be inmediately preceded by an if(){} block. Setting a g_counter in betwwen makes next else an error.

    To count comparisons you can use the comma operator which returns the result of the evaluation of E2 in (E1,E2)

    So you can code like this:

    if (++g_counter, userinput < 400){
        cout << "Wavelength to small, try again"<< endl;
        
        cout << "You evauluated this many conditions: "<< g_counter << endl; 
    }
    
    else if ((++g_counter, userinput >= 400) && (++g_counter, userinput <=445)){
        cout<<  "Your wavelength corresponds to the color violet"<< endl;
    
        cout << "You evauluated this many conditions: "<< g_counter << endl; 
    }
    
    else....
    

    Note the parentesis when && is used (two comparisions)