-1

The loop works fine if I do not have the if statement, but once I add it in, the code no longer runs. It doesn't have a problem when building. I can tell that I didn't initialize repNum when I run it.

The code as follow:

#include<iostream>
using namespace std;

int main()
{
    int repNum;
    int prodSold;
    int prodPrice;
    int repTotal;
    int numReps = 0;
    if (repNum >0 && repNum < 21)
    {
        for(int numReps = 0; numReps <= 20; numReps ++)
        {
            cin >> repNum; 
            cin >> prodSold;
            cin >> prodPrice;
            repTotal = prodSold * prodPrice;
            cout << "Agent #" << repNum << " sold " << prodSold << " packages at $" << prodPrice << 
                " for a dollar value of $" << repTotal << endl;
        }
    }
    else
    {
        cout << "***Invalid Agent***" << endl;
    }
}
Tas
  • 7,023
  • 3
  • 36
  • 51
Thomas Zhu
  • 35
  • 3
  • why you didnt initialize repNum? you have to for making it work. – Syeda Zunaira Mar 31 '16 at 05:17
  • Everybody else has diagnosed your problem. But in addition, you have declared two separate copies of `numReps`. This doesn't cause a problem here, but it will in your future code if you don't take time to understand it. Because `numReps` is already declared, your `for`-loop should start `for(numReps = 0;...` – TonyK Mar 31 '16 at 12:10

5 Answers5

2

When condition if (repNum >0 && repNum < 21) is executed, variable repNum is still uninitialized, that's why you get error.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79
1

You are experiencing undefined behaviour by using an unitialised variable:

int repNum;
if (repNum >0 && repNum < 21)

repNum is never initialised with a value! You need to give it a value.

int repNum = 0;

You should also consider using auto as it will force you to initialise it to a value:

auto repNum = 0u;

If you tried the following

auto repNum;

You would get a compiler error.

As it currently is, you should get a warning when you compile. If you turn your compiler warning settings up, you should get a warning about this.

Community
  • 1
  • 1
Tas
  • 7,023
  • 3
  • 36
  • 51
0

You should initialize repNum. Otherwise you won´t be able to compare repNum greater 0, because repNum has no value. If you want it like that just set repNum -1
By the way asking for repNum in the if statement only would make sens if you had a value for it coming from an other point of the programm. Are you missing code here?

Marcel Theis
  • 303
  • 1
  • 12
0

Non-Intialization of a non-static variable leads to the variable maybe getting assigned some garbage values.

In your case, you haven't initialized repNum, which might get assigned some garbage value. You then go ahead to check this garbage values against some expression in your if statement, which might lead to undefined behavior.

AdityaD
  • 11
  • 2
0

As you said it's an unitialized variables problem. In this case you can use do while for if check. So, use do while to get input from user for all variables, then when the condition accepted you have permission to go for a for loop.

Suppose you wanted to get valid input 20 times:

int i = 0;
do
{
    cin >> repNum; 
    if (repNum >0 && repNum < 21)
    {
        for(int numReps = 0; numReps <= 20; numReps ++)
        {
            cin >> prodSold;
            cin >> prodPrice;
            repTotal = prodSold * prodPrice;
        }
        i++;
    }
    else
    {
        cout << "***Invalid Agent***" << endl;
        i--;
    }  
} while (i < 20);

You may want to check for i being a negative value in the else case. If i < 0 is unacceptable you can use whatever error handling you prefer.

Tas
  • 7,023
  • 3
  • 36
  • 51
Vurgun M
  • 97
  • 1
  • 5