Sign in to follow this  
NUCLEAR RABBIT

Need Help with C++ [FIXED]

Recommended Posts

NUCLEAR RABBIT    318
Ok, So i tried making a program that gets the users info, and tell them what grade their in. The program compiles with no errors, but when i run the program, when it says the summary (when it says what a=grade their in), it always says "0 Grade". Please, someone help me.
/**$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*$
$*%    PROGRAMMER NAME: BRANDON WALL   *$%
%*$    DATE:  4-15-06                  %*$
*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$**/ 

// Practice Using Funtions #2

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


      //Declaring Variables
      string name;
      string school;
      int age;
      int ageGrade1;
      int grade = age - ageGrade1;
      
              
void names()
{
     
     cout << "Please type in your Full name: \n";
     cin >> name;
     cout << "Now, Please type in your Age: \n";
     cin >> age;
     cout << "Please type in your Age when you started the 1st Grade: \n";
     cin >> ageGrade1;
     cout << "Now, Please type in your Current School Name: \n";
     cin >> school;
     system("cls");
     cout << "Thank You";
     system("cls");
     
}

void summary(string name, string school, int age, int ageGrade1, int grade)
{
     
     cout << "Ok, "
          << name
          << ". Your "
          << age
          << " old and your in "
          << grade
          << " Grade, and go to "
          << school
          << "!"
          << "\n\n";
          
}

int main()
{
    
    names();
    summary(name, school, age, ageGrade1, grade);
    system("pause");
    return 0;
    
}

Thanx [Edited by - NUCLEAR RABBIT on April 15, 2006 5:24:03 PM]

Share this post


Link to post
Share on other sites
cherryhouse    100
[source lang="cpp" ][/source ]
Remove the spaces between " ] and e ] at the end of each tag, I just added them so you could see the tags.


void summary(string name, string school, int age, int ageGrade1, int grade)
{
grade = age - ageGrade1;
cout << "Ok, "
<< name
<< ". Your "
<< age
<< " old and your in "
<< grade
<< " Grade, and go to "
<< school
<< "!"
<< "\n\n";

}



Make sure you set the value of grade AFTER you get the users input. When you instantiate grade like so: int grade = age - ageGrade1; ... that's not going to "monitor" grade to make sure it always == age - ageGrade1.

Share this post


Link to post
Share on other sites
Tera_Dragon    260
You need to calculate the grade after you get the input from the user, not outside of a function scope.
So move grade = age - ageGrade1; to the end of names()

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by cherryhouse
*** Source Snippet Removed ***


If you want to show the tags, you have to use the HTML entity characters: &#91source&#93 &#91/source&#93 More info here.

That gives you [source] [/source] [wink]

Share this post


Link to post
Share on other sites
nullsquared    126
Quote:
Original post by Tera_Dragon
You need to calculate the grade after you get the input from the user, not outside of a function scope.
So move grade = age - ageGrade1; to the end of names()


To elaborate, grade is getting a really funky value... When you declare something without initialising it gets a funky value like that... So since age is a funky value (lets say... 3452) for now and so is ageGrade1 (1234 - just something), it turns out like 3452 - 1234 so you get a funky value for grade, too.

Share this post


Link to post
Share on other sites
jonahrowley    300
int grade = age - ageGrade1;


Again, we get into scoping and variable initialization. When you declare a variable, you have a chance to initialize it.
int grade;
and
int grade = 7;
are the initialized and uninitialized forms (plus the constructor form, but we'll just ignore that for now). The initialization happens as soon as the declaration is encountered (more or less). What happens when you use variables who haven't been initialized in an initialization of another variable (as you have done)? The behavior is undefined (which means it depends on the compiler and execution environment), but since these are globals variables, they live in a 0-initialized memory page, and will always start out as 0. Long story short, you should have gotten warnings about using an uninitalized variable. Pay attention to your warnings! Your programs should compile with 0 warnings! Add that to a list of things to post when asking questions :P

So, as someone already pointed out, you need to do that calculation after you fill the variables with something meaningful and leave it uninitialized until then.

Share this post


Link to post
Share on other sites
cherryhouse    100
Quote:
To elaborate, grade is getting a really funky value... When you declare something without initialising it gets a funky value like that... So since age is a funky value (lets say... 3452) for now and so is ageGrade1 (1234 - just something), it turns out like 3452 - 1234 so you get a funky value for grade, too.


To elaborate, when he says funky value, he means if you don't initially set the value of the int when it is declared, the value will be set to a value in a random memory address.

EDIT:@OP: What happens when you enter "fifteen" when the program asks you for your age? You can't control what your user enters into the program, you have to create solutions for these problems. This could be your next practice.

Also, don't get so used to using global variables, they are a bad habbit which will most likely need to be dropped when you enter the world of OOP. It could be a little bit tough to do if you use them in every program you've done before OO.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this