Errors from a Program I wrote

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LilMeechc20
    New Member
    • Sep 2008
    • 7

    Errors from a Program I wrote

    I am writing a program for class and I keep getting error messages that I am not declaring variables that are clearly declared. I only get this message when I write a program that requires a input from the user. Below is my code that I wrote and the error message can someone tell me what am I doing wrong? I am using Visual Basic and C++.

    Thanks in advance.....

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	const float TAXRATE = 0.06;
    	float totalCost, total_discount, taxDue, amountDue;
    	float cost;
    	float num_items;
    	float discountRate;
    	
    	totalCost = num_items * cost;
    	total_discount = totalCost - discountRate * totalCost;
    	taxDue = total_discount * TAXRATE;
    	amountDue = total_discount + taxDue;
    
    	cout<<"Please enter the Cost of all Items: \n";
    	cin >> cost;
    	cout<<"Please enter the Numbr of Items: \n";
    	cin >> num_items;
    	cout<<"Please enter the Discounted Rate: \n";
    	cin >> discountRate;
    
    	cout<<"The Total Cost is: "<<totalCost<<" .\n";
    	cout<<"The Tax Due: "<<taxDue<<" .\n";
    	cout<<"The Amount Due: "<<amountDue<<" \n";
    
    	return 0;
    }
    1>------ Rebuild All started: Project: Q10p175, Configuration: Debug Win32 ------
    1>Deleting intermediate and output files for project 'Q10p175', configuration 'Debug|Win32'
    1>Compiling...
    1>Q10p175.cpp
    1>c:\documents and settings\dj0000 34880\desktop\c omputer science\lab assignment 2\q10p175\q10p1 75\q10p175.cpp( 8) : warning C4305: 'initializing' : truncation from 'double' to 'const float'
    1>c:\documents and settings\dj0000 34880\desktop\c omputer science\lab assignment 2\q10p175\q10p1 75\q10p175.cpp( 14) : warning C4700: uninitialized local variable 'num_items' used
    1>c:\documents and settings\dj0000 34880\desktop\c omputer science\lab assignment 2\q10p175\q10p1 75\q10p175.cpp( 14) : warning C4700: uninitialized local variable 'cost' used
    1>c:\documents and settings\dj0000 34880\desktop\c omputer science\lab assignment 2\q10p175\q10p1 75\q10p175.cpp( 15) : warning C4700: uninitialized local variable 'discountRate' used
    1>Compiling manifest to resources...
    1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
    1>Copyright (C) Microsoft Corporation. All rights reserved.
    1>Linking...
    1>LINK : C:\Documents and Settings\dj0000 34880\Desktop\C omputer Science\Lab Assignment 2\Q10p175\Debug \Q10p175.exe not found or not built by the last incremental link; performing full link
    1>Embedding manifest...
    1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
    1>Copyright (C) Microsoft Corporation. All rights reserved.
    1>Build log was saved at "file://c:\Documents and Settings\dj0000 34880\Desktop\C omputer Science\Lab Assignment 2\Q10p175\Q10p1 75\Debug\BuildL og.htm"
    1>Q10p175 - 0 error(s), 4 warning(s)
    ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
  • archonmagnus
    New Member
    • Jun 2007
    • 113

    #2
    It's not that your variables are not declared, it's that they are undefined. When you declare a variable, it will not automatically have a useful value. In fact, it's almost always garbage values that are left at a particular memory address.

    To give you an example of what you are doing, compile this code:
    [code=cpp]
    #include <iostream>

    using namespace std;

    int main (void)
    {
    // Declare two values, but do not initialize them
    int foo, bar;

    // Bar is uninitialized so foo will be a garbage value
    foo = bar + 65;

    cout<<foo;

    return;
    }
    [/code]

    If you were to initialize "bar" to, say, zero, "foo" would then have a value of 65. But, as the current code stands, you would simply have an erroneous value of "foo".

    In the case of your program, you are performing operations with uninitialized values. Prompt the user for input first (giving the variables useful values), then perform the operations with those values.

    Comment

    • LilMeechc20
      New Member
      • Sep 2008
      • 7

      #3
      uh, I see what you mean now. So all I have to do is move the total cost, etc to follow the code asking for the input, that I should be ok?

      That is what I did and I didnt have any problems.

      Thank you so much for your help with this matter....

      Comment

      Working...