Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,585 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,978 people online right now. Registration is fast and FREE... Join Now!




Flag help

 
Reply to this topicStart new topic

Flag help

brianlb
5 Oct, 2008 - 01:17 PM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 21

When i run this program is get the error message anytime I enter anything less than 0 in " Run Time Check Failure 3 - the variable total is being used without being initialized. Any help on how to fix this? I have yet to finish the program but just testing pieces as I write it. Thx

CODE

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int numSold;
    double total;
    const double PRICE = 99.00,
                 DISCPRICE20 = 79.20,
                 DISCPRICE30 = 69.30,
                 DISCPRICE40 = 59.40,
                 DISCPRICE50 = 49.50;
    bool invalidNum = false;

    cout << fixed << showpoint << setprecision(2);

    cout << "Enter the number of units sold: ";
    cin >> numSold;

    
    if (numSold <= 0)
        invalidNum = true;
    if (invalidNum)
        cout << "You must enter a number greater than zero.\n";

    else if (numSold < 10 && numSold >= 1)
    {
        total = numSold * PRICE;
    }
    else if (numSold >= 10 && numSold <= 19)
    {
        total = numSold * DISCPRICE20;
    }
    else if (numSold >=20 && numSold <= 49)
    {
        total = numSold * DISCPRICE30;
    }
    else if (numSold >= 50 && numSold <= 99)
    {
        total = numSold * DISCPRICE40;
    }
    else if (numSold >= 100)
    {
        total = numSold * DISCPRICE50;
    }

    cout << "Your total is: " << total << endl;
        return 0;
}


This post has been edited by brianlb: 5 Oct, 2008 - 01:25 PM
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Flag Help
5 Oct, 2008 - 01:25 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,026



Thanked: 82 times
Dream Kudos: 1175
My Contributions
I think this should help:
CODE
int numSold = 0;
    double total = 0;


Basically, now each of those values have a default value (and as such they shouldn't have any trouble with not being set when you use them).

HTH
User is offlineProfile CardPM
+Quote Post

brianlb
RE: Flag Help
5 Oct, 2008 - 01:29 PM
Post #3

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 21

thx, that worked, should I always have a variable set at a default value of 0 if it's value is going to be changing when the program is ran?
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Flag Help
5 Oct, 2008 - 01:37 PM
Post #4

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,026



Thanked: 82 times
Dream Kudos: 1175
My Contributions
Not always, it is just when you don't have it set to an actual number and are using it in an if statement or a loop.

See, when you are using this code it is using a function to take user input, meaning that until it gets input the variable has no value, and that isn't allowed in compilation, so, if you are taking user input you will need to set an initial value, but if you are doing something like calling a function like sum(5, 6); and setting it to the value (like below) it should work:

CODE

int sum(int a, int b){
  retrn a+b;
}
int main(){
  int test = sum(5, 6);
  return 0;
}


Hope that makes sense smile.gif
User is offlineProfile CardPM
+Quote Post

brianlb
RE: Flag Help
5 Oct, 2008 - 01:39 PM
Post #5

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 21

yep, that makes sense, thx a lot.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month