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

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




getting numbers in wrong format

2 Pages V  1 2 >  
Reply to this topicStart new topic

getting numbers in wrong format

hlabrams
post 27 Aug, 2006 - 02:24 PM
Post #1


D.I.C Head

**
Joined: 17 Aug, 2006
Posts: 66


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

tufan
post 27 Aug, 2006 - 02:45 PM
Post #2


New D.I.C Head

Group Icon
Joined: 26 Aug, 2006
Posts: 4



Dream Kudos: 60
My Contributions


u can declare the variables as double or float wink2.gif

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); wink2.gif

hope it helped rolleyes.gif
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 27 Aug, 2006 - 02:49 PM
Post #3


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


well from all i can tell you dont properly initialize the variables. Post the code and we can help you more.
User is offlineProfile CardPM

Go to the top of the page

hlabrams
post 27 Aug, 2006 - 03:30 PM
Post #4


D.I.C Head

**
Joined: 17 Aug, 2006
Posts: 66


My Contributions


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.

QUOTE(tufan @ 27 Aug, 2006 - 03:45 PM) *

u can declare the variables as double or float wink2.gif

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); wink2.gif

hope it helped rolleyes.gif


Ok, I'll try that. I'll have to look back at my textbook. Thank you!
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Aug, 2006 - 05:19 AM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

hlabrams
post 28 Aug, 2006 - 05:51 AM
Post #6


D.I.C Head

**
Joined: 17 Aug, 2006
Posts: 66


My Contributions


QUOTE(Amadeus @ 28 Aug, 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.


How and where do I do that? I assigned a 0 value to them in the struct but got 20 errors.
User is offlineProfile CardPM

Go to the top of the page

hlabrams
post 28 Aug, 2006 - 10:10 AM
Post #7


D.I.C Head

**
Joined: 17 Aug, 2006
Posts: 66


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Aug, 2006 - 10:30 AM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Can you repost the code?
User is offlineProfile CardPM

Go to the top of the page

hlabrams
post 28 Aug, 2006 - 04:49 PM
Post #9


D.I.C Head

**
Joined: 17 Aug, 2006
Posts: 66


My Contributions


QUOTE(Amadeus @ 28 Aug, 2006 - 11:30 AM) *

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);

}

}
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 28 Aug, 2006 - 05:45 PM
Post #10


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


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!
User is offlineProfile CardPM

Go to the top of the page

DeeViLiSh
post 28 Aug, 2006 - 10:12 PM
Post #11


D.I.C Head

Group Icon
Joined: 25 Jul, 2006
Posts: 175



Thanked 1 times

Dream Kudos: 575
My Contributions


QUOTE(Mrafcho001 @ 28 Aug, 2006 - 06:45 PM) *

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 :
CODE
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 :
CODE
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 [code][/code] around your code.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Aug, 2006 - 04:33 AM
Post #12


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/23/08 03:30AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month