Home > Mobile >  why does my code not read or follow the if statements?
why does my code not read or follow the if statements?

Time:11-15

Why does my code not read or follow the if statements?

#include<iostream>
using namespace std;

int main (){
    
    int sum = 0;
    int integer;
    
    while ((integer != -1) && (sum < 2000)){
        int integer;
        
        cout<<"Enter an integer: ";
        cin>>integer;

        if (integer == -1){
            cout<<"Program was terminated.\n"<<"Your total sales are: "<<sum<<endl;
            break;
        }
        else if (sum >= 2000) {
            cout<<"Congratulations!"<<"Your total sales are: "<<sum<<endl;
            break;
        }
        
        sum = sum   integer;
        if (sum > 499){
            cout<<"You're off to a good start!"<<endl;
        }
        if (sum > 999){
            cout<<"You're halfway through!"<<endl;
        }
        else if (sum > 1499){
            cout<<"You're almost there!"<<endl;
        }
    }
    return 0;
}

Expected: Input looks like: Enter an interger: 350, 500,800, 500

Then the out should look like:

Enter an integer 350
      " "        500
                 youre off to a good start
                 800
                 youre halfway through
                 500
                 Congratualations! Youre total sales are: 2150

Reality:

Enter an integer: 350
Enter an integer: 500
You're off to a good start!
Enter an integer: 800
You're off to a good start!
You're halfway through!
Enter an integer: 500
You're off to a good start!
You're halfway through!

CodePudding user response:

you want only 1 branch to be entered, so you need a if else if chain.

if (sum >= 2000) {
    std::cout << "Congratulations!" << "Your total sales are: " << sum << std::endl;
    break;
}
else if (sum > 1499) {
    std::cout << "You're almost there!" << std::endl;
}
else if (sum > 999) {
    std::cout << "You're halfway through!" << std::endl;
}
else if (sum > 499) {
    std::cout << "You're off to a good start!" << std::endl;
}

You have to start with the largest number to the smallest one, since if you go from smaller to larger, if a branch is entered it will always be the first branch.


while ((integer != -1) && (sum < 2000)){

this is wrong, as integer is not initialiazed and this is therefore undefined behaviour. In some cases it might never enter the while loop because integer holds some garbage value.

You can replace it with a simple while true loop:

while (true) {

since you are calling break; in the final branches anyway (this escapes the while loop). This will also fix the bug, that the loop would terminate before the final print outs.


int integer;

while ((integer != -1) && (sum < 2000)) {
    int integer;

you just declared integer twice, the second one is not needed.


Full code:

#include <iostream>

int main() {
    int sum = 0;
    int integer;

    while (true) {
        std::cout << "Enter an integer: ";
        std::cin >> integer;

        if (integer == -1) {
            std::cout << "Program was terminated.\n" << "Your total sales are: " << sum << std::endl;
            break;
        }

        sum = sum   integer;
        if (sum >= 2000) {
            std::cout << "Congratulations!" << "Your total sales are: " << sum << std::endl;
            break;
        }
        else if (sum > 1499) {
            std::cout << "You're almost there!" << std::endl;
        }
        else if (sum > 999) {
            std::cout << "You're halfway through!" << std::endl;
        }
        else if (sum > 499) {
            std::cout << "You're off to a good start!" << std::endl;
        }
    }
}

read Why is "using namespace std;" considered bad practice?

  •  Tags:  
  • c
  • Related