• 9
• 9
• 10
• 10
• 11

# Need Help with C++ [FIXED]

This topic is 4388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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;

void names()
{

cin >> name;
cin >> age;
cin >> school;
system("cls");
cout << "Thank You";
system("cls");

}

{

cout << "Ok, "
<< name
<< age
<< " old and your in "
<< " Grade, and go to "
<< school
<< "!"
<< "\n\n";

}

int main()
{

names();
system("pause");
return 0;

}


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

##### Share on other sites
[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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by Tera_DragonYou 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 on other sites
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 on other sites
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.