getting numbers in wrong format

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1801 Views - Last Post: 29 August 2006 - 12:19 PM Rate Topic: -----

#1 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

getting numbers in wrong format

Posted 27 August 2006 - 03:24 PM

Can someone tell me why, when I display a table as part of a C++ code, for the cells that display the result of a computation (example: an average), I get a really long number (example: 920000572450000.00) and that number is a negative. It should display a positive number, such as 98.25 or 100.00. Also, I need to be able to take the names that I input into the table and display alphabetically. How can I do that? Any help would be very much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: getting numbers in wrong format

#2 tufan  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 06

Re: getting numbers in wrong format

Posted 27 August 2006 - 03:45 PM

u can declare the variables as double or float ;)

if u want to alphabetically sort them you can use the algorithm class for it. but you must add that header file to do it.

if u use a string or a vector:

u can do it by this way : sort(str) or sort(vec); ;)

hope it helped :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#3 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: getting numbers in wrong format

Posted 27 August 2006 - 03:49 PM

well from all i can tell you dont properly initialize the variables. Post the code and we can help you more.
Was This Post Helpful? 0
  • +
  • -

#4 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

Re: getting numbers in wrong format

Posted 27 August 2006 - 04:30 PM

Here's my code:

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cmath>

using namespace std;

struct STUDENT_STRUCT
{
char last_name[31];
char first_name[31];
char soc_sec_no[10];
char ph_no[11];
int quiz_1;
int quiz_2;
int quiz_3;
int quiz_4;
int quiz_5;
double quiz_avg;
int final_exam;
int final_grade;
double class_avg;

};

int Load_Student_Table(STUDENT_STRUCT[], int);
void Display_Student_Table(STUDENT_STRUCT[], int);
double Calc_Quiz_Avg(STUDENT_STRUCT[], double);
int Calc_Final_Grade(STUDENT_STRUCT[], int);
double Calc_Class_Avg(STUDENT_STRUCT[], double);


const int TABLE_SIZE = 25;


int main()
{


STUDENT_STRUCT student_table[TABLE_SIZE];


cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);

int num_students;
num_students=Load_Student_Table(student_table, TABLE_SIZE);

// Display the table
cout << endl << endl;
cout << "This is the table you entered. ";

Display_Student_Table(student_table, num_students);

cout << endl << endl;
cin >> num_students;
return 0;
}

int Load_Student_Table(STUDENT_STRUCT student_table[], int num_students)
{
int row=0;
int response=1;

cout << endl;
cout << "Enter the table values as you are prompted:"
<< endl << endl;

while(response==1)
{

cout << endl;
cout << "For row #" << row+1 << " enter:" << endl;

cout << "Last Name: ";
cin.getline(student_table[row].last_name, 31);

cout << "First Name: ";
cin.getline(student_table[row].first_name, 31);

cout << "Social Security Number: ";
cin.getline(student_table[row].soc_sec_no, 10);

cout << "Phone Number: ";
cin.getline(student_table[row].ph_no, 11);

cout << "Quiz 1: ";
cin >> student_table[row].quiz_1;

cout << "Quiz 2: ";
cin >> student_table[row].quiz_2;

cout << "Quiz 3: ";
cin >> student_table[row].quiz_3;

cout << "Quiz 4: ";
cin >> student_table[row].quiz_4;

cout << "Quiz 5: ";
cin >> student_table[row].quiz_5;

cout << "Final Exam: ";
cin >> student_table[row].final_exam;

row++;
cout<<"Do you wish to enter another student? Enter 1 for yes or 0 for no: "<<endl;
cin>>response;
}

return row;
}
double Calc_Quiz_Avg(STUDENT_STRUCT student_table[])
{
const int num_quizzes=5;
int quiz_1=0;
int quiz_2=0;
int quiz_3=0;
int quiz_4=0;
int quiz_5=0;
double quiz_avg= double(quiz_1+quiz_2+quiz_3+quiz_4+quiz_5)/5;
return quiz_avg;
}
int Calc_Final_Grade(STUDENT_STRUCT student_table[])
{

int final_exam=0;
double quiz_avg=0.0;
const double QUIZ_AVG_WEIGHT=0.75;
const double FINAL_EXAM_WEIGHT=0.25;
int final_grade;
final_grade=(double(QUIZ_AVG_WEIGHT*quiz_avg) + double(FINAL_EXAM_WEIGHT*final_exam));
return final_grade;
}
double Calc_Class_Avg(STUDENT_STRUCT student_table[])
{
int final_grade=0;
int num_students=0;
double total=0.0;
double class_avg;
class_avg=total+=final_grade/num_students;
return class_avg;
}
void Display_Student_Table(STUDENT_STRUCT student_table[], int num_students)
{
int row;

cout << endl << endl;
cout << setw(14) << left << "Last Name"
<< setw(14) << left << "First Name"
<< setw(14) << left << "Final Exam"
<< setw(14) << left << "Quiz Average"
<< setw(14) << left << "Final Grade" << endl;

for(row=0; row<num_students; ++row)
{
cout << setw(14) << student_table[row].last_name
<< setw(14) << student_table[row].first_name
<< setw(14) << student_table[row].final_exam
<< setw(14) << student_table[row].quiz_avg
<< setw(14) << student_table[row].final_grade << endl;

}

}

I've also noticed that if I enter 1 to input data on another student, it looks like this..... Last Name: First Name: instead of Last Name:
First Name:

I also needed to add a function that takes the total of all final grades to find the avg. The number of final grades that have to be added together is unknown since the number of students is an unknown until the table has been loaded. I'm so confused!! I don't know if I wrote that function correctly.

View Posttufan, on 27 Aug, 2006 - 03:45 PM, said:

u can declare the variables as double or float ;)

if u want to alphabetically sort them you can use the algorithm class for it. but you must add that header file to do it.

if u use a string or a vector:

u can do it by this way : sort(str) or sort(vec); ;)

hope it helped :rolleyes:


Ok, I'll try that. I'll have to look back at my textbook. Thank you!
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: getting numbers in wrong format

Posted 28 August 2006 - 06:19 AM

Similar to the other post, the variables have been declared, but have no been initialized. You have declared them in the structure, but have never assigned values to them.
Was This Post Helpful? 0
  • +
  • -

#6 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

Re: getting numbers in wrong format

Posted 28 August 2006 - 06:51 AM

View PostAmadeus, on 28 Aug, 2006 - 06:19 AM, said:

Similar to the other post, the variables have been declared, but have no been initialized. You have declared them in the structure, but have never assigned values to them.


How and where do I do that? I assigned a 0 value to them in the struct but got 20 errors.
Was This Post Helpful? 0
  • +
  • -

#7 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

Re: getting numbers in wrong format

Posted 28 August 2006 - 11:10 AM

I initialized the variables but still get a really long number. I changed all types to int, not double....just to avoid any conversion issues. That didn't change how the code executed anyway.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: getting numbers in wrong format

Posted 28 August 2006 - 11:30 AM

Can you repost the code?
Was This Post Helpful? 0
  • +
  • -

#9 hlabrams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 17-August 06

Re: getting numbers in wrong format

Posted 28 August 2006 - 05:49 PM

View PostAmadeus, on 28 Aug, 2006 - 11:30 AM, said:

Can you repost the code?


I sure can, but now, the quiz_avg displays fine. It's the final_grade that still appears wrong in the table. I switched some things back to doubles, not ints, so you will see type double in the code. I fixed the function Calc_Final_Grade so it was just like Calc_Quiz_Avg, but that didn't fix the problem.

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<cstring>

using namespace std;

struct STUDENT_STRUCT
{
char last_name[31];
char first_name[31];
char soc_sec_no[10];
char ph_no[11];
int quiz_1;
int quiz_2;
int quiz_3;
int quiz_4;
int quiz_5;
double quiz_avg;
int final_exam;
int final_grade;
int class_avg;

};

int Load_Student_Table(STUDENT_STRUCT[], int);
void Display_Student_Table(STUDENT_STRUCT[], int);
double Calc_Quiz_Avg(STUDENT_STRUCT[], int);
int Calc_Final_Grade(STUDENT_STRUCT[], int);
int Calc_Class_Avg(int, int);
void Sort_Table(STUDENT_STRUCT[], int);

const int TABLE_SIZE = 25;

int main()
{

STUDENT_STRUCT student_table[TABLE_SIZE];


cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);

int num_students;
num_students=Load_Student_Table(student_table, TABLE_SIZE);

Calc_Quiz_Avg(student_table, num_students);

// Display the table
cout << endl << endl;
cout << "This is the table you entered: ";

Display_Student_Table(student_table, num_students);

cout << endl << endl;
cin >> num_students;
return 0;
}

int Load_Student_Table(STUDENT_STRUCT student_table[], int num_students)
{
int row=0;
int response=1;


cout << endl;
cout << "Enter the table values as you are prompted:"
<< endl << endl;

while(response==1)
{

cout << endl << endl;
cout << "For row #" << row+1 << " enter:" << endl << endl;

cout << "Last Name: ";
cin.getline(student_table[row].last_name, 31);

cout << "First Name: ";
cin.getline(student_table[row].first_name, 31);

cout << "Social Security Number: ";
cin.getline(student_table[row].soc_sec_no, 10);

cout << "Phone Number: ";
cin.getline(student_table[row].ph_no, 11);

cout << "Quiz 1: ";
cin >> student_table[row].quiz_1;

cout << "Quiz 2: ";
cin >> student_table[row].quiz_2;

cout << "Quiz 3: ";
cin >> student_table[row].quiz_3;

cout << "Quiz 4: ";
cin >> student_table[row].quiz_4;

cout << "Quiz 5: ";
cin >> student_table[row].quiz_5;

cout << "Final Exam: ";
cin >> student_table[row].final_exam;

row++;
cout<<"Do you wish to enter another student? Enter 1 for yes or 0 for no: "<<endl;
cin>>response;
}

return row;
}
double Calc_Quiz_Avg(STUDENT_STRUCT s[], int num_students)
{
const int num_quizzes=5;
int row;

for(row=0; row<num_students; ++row)
s[row].quiz_avg=double(s[row].quiz_1+s[row].quiz_2+s[row].quiz_3+s[row].quiz_4+s[row].quiz_5)/num_quizzes;

return s[row].quiz_avg;
}
int Calc_Final_Grade(STUDENT_STRUCT, int num_students)
{
int row;
int final_exam=0;
double final_grade=0.0;
double quiz_avg=0.0;

const double QUIZ_AVG_WEIGHT=0.75;
const double FINAL_EXAM_WEIGHT=0.25;

for(row=0; row<num_students; ++row)
final_grade=(QUIZ_AVG_WEIGHT*quiz_avg) + (FINAL_EXAM_WEIGHT*final_exam);
return final_grade;
}
int Calc_Class_Avg(STUDENT_STRUCT, int num_students)
{
int final_grade=0;
int total=0;
int class_avg;
class_avg=total+=final_grade/num_students;
return class_avg;
}
void Display_Student_Table(STUDENT_STRUCT student_table[], int num_students)
{
int row;

cout << endl << endl;
cout << setw(14) << left << "Last Name"
<< setw(14) << left << "First Name"
<< setw(14) << left << "Final Exam"
<< setw(14) << left << "Quiz Average"
<< setw(14) << left << "Final Grade" << endl;

for(row=0; row<num_students; ++row)
{
cout << setw(14) << student_table[row].last_name
<< setw(14) << student_table[row].first_name
<< setw(14) << student_table[row].final_exam
<< setw(14) << student_table[row].quiz_avg
<< setw(14) << student_table[row].final_grade << endl;
void Sort_Table(STUDENT_STRUCT student_table[], int num_students);
cout << endl << endl;
cout << "The table sorted alphabetically by last name is:";

void Display_Student_Table(STUDENT_STRUCT student_table[], int num_students);
cout << endl << endl;
cout << setw(14) << left << "Last Name"
<< setw(14) << left << "First Name"
<< setw(14) << left << "Final Exam"
<< setw(14) << left << "Quiz Average"
<< setw(14) << left << "Final Grade" << endl;

for(row=0; row<num_students; ++row);

}

}
Was This Post Helpful? 0
  • +
  • -

#10 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: getting numbers in wrong format

Posted 28 August 2006 - 06:45 PM

you never call Calc_Final_Grade... and you never set the final_grade variable in the STUDENT_STRUCT structure.

Its a very messy code i'd recommend a re-structure. and also use the CODE tags!
Was This Post Helpful? 0
  • +
  • -

#11 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: getting numbers in wrong format

Posted 28 August 2006 - 11:12 PM

View PostMrafcho001, on 28 Aug, 2006 - 06:45 PM, said:

Its a very messy code i'd recommend a re-structure. and also use the CODE tags!

Lol, took a while to just sort out the code xD

Anyways, it's like the Calc_Quiz_Avg problem you had earlier, you need to include hte function somewhere for it to work...
And the function itself isn't properly setup too :
int Calc_Final_Grade(STUDENT_STRUCT s[], int num_students)
{
		int row;
		const double QUIZ_AVG_WEIGHT=0.75;
		const double FINAL_EXAM_WEIGHT=0.25;

		for(row=0; row<num_students; ++row)
		s[row].final_grade=(double(QUIZ_AVG_WEIGHT * s[row].quiz_avg) + double(FINAL_EXAM_WEIGHT * s[row].final_exam));

		return s[row].final_grade;
}
fixed.

Update :
int main()
{


		STUDENT_STRUCT student_table[TABLE_SIZE];


		cout << setprecision(2)
				<< setiosflags(ios::fixed)
				<< setiosflags(ios::showpoint);

		int num_students;
		num_students=Load_Student_Table(student_table, TABLE_SIZE);

		Calc_Quiz_Avg(student_table, num_students);
		Calc_Final_Grade(student_table, num_students);

		// Display the table
		cout << endl << endl;
		cout << "This is the table you entered. ";

		Display_Student_Table(student_table, num_students);

		cout << endl << endl;
		cin >> num_students;
		return 0;
}


Code tags are

around your code.
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: getting numbers in wrong format

Posted 29 August 2006 - 05:33 AM

For both the calculation functions, you have declared them as returning type int, and in fact you return those values, but they are never assigned anywhere. Since the assignment to the appropriate sturcture member occurs withion the function itself, I would advise that no return type is required, as it's not being used.
Was This Post Helpful? 0
  • +
  • -

#13 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: getting numbers in wrong format

Posted 29 August 2006 - 06:08 AM

I almost understand what you're talking about, in other words, take out the return s[row].final_grade since in int main(), it's not asking for it...
K, thx Amadeus, I learnt something ^_^
Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: getting numbers in wrong format

Posted 29 August 2006 - 06:31 AM

Exactly...you've made the proper assignment in the function itself, so you don;t need to return anything. That final return can be removed, and the return type of the functions changed to void.
Was This Post Helpful? 0
  • +
  • -

#15 DeeViLiSh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 175
  • Joined: 25-July 06

Re: getting numbers in wrong format

Posted 29 August 2006 - 09:06 AM

However, if I were to type cout<<Calc_Quiz_Avg(student_table[], num_students);
It would print out s[row].quiz_avg?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2