hello guys, i'm just new here, can someone help me whats wrong with this code?
I was suppose to input a name of a student then each student has 5 scores then I am to compute each students average then also output the highest average. This is the code :
#include<stdio.h>
#include<conio.h>
int main()
{
char stud_name;
float avescore_student=0;
float score_total=0;
float highavescore=0;
int avetotal=0;
int score=0;
float total;
int i, j, k, o, g, y=0;
for(i=1; i<=10; i++)
{
printf("Input name %d:\n",i);
scanf("%c", &stud_name);
First, When I ran your program the compiler complained about your undefined variables. For
the future I would suggest setting them to 0 or some value so that you know the initial states for all of your variables.
Second, you are using a char variable to input more than one character which will overload the buffer. An example would be if you used the name "sam" as input. The character 's' would be saved in the variable and 'a' and 'm' would go straight to the buffer. When the program asks for another input it will automatically assign what was left in the buffer without asking. Thus 'a' and 'm' would be put in as your first two score inputs. Since you are trying to assign a letter to a number variable your program will output junk. To correct this problem use a string variable which will store a word instead of one character; You can also use a character array
which can hold as many characters as you define.
Third, every time you ran the loop for reading in the scores, you never asked for the name input again. I didn't notice this the first couple times I ran the program, but you need to implement an outer loop to control every time you enter a new name otherwise you will be entering a letter into a number variable similar to what was happening before.
I hope this helps. If I get more time away from homework I might give it another go.
I believe I left enough comments for you to be able to work with, but if you have any more questions let me know and I will try to help the best I can.
Make a program to input 10 names of student. Each student has 5 exam scores. Determine the average exam score of each student. Output the highest average exam score.
I already change all the code for readability and i can loop a name but the looping the average is not working for example : when i input a name "a" then its score is "1,2,3,4,5" and its average is "3" , which is correct but the next name to be input is "b" then its scores is "1,2,3,4,5" and its average that become "6" which is wrong. And also not giving me the proper highest average score. Am i lacking a command here? or what?.. here is the code :
#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
char name;
int score;
int sum=0;
float ave=0,max=0;
for(x=1;x<=4;x++)
{
printf("Enter name %d : \n", x);
scanf("%s", &name);
for(y=1;y<=5;y++)
{
printf("Enter score %d :", y);
scanf("%d", &score);
sum=sum+score;
}
ave=sum/5;
printf("The average score is %.2f \n", ave);
}
if(ave>max)
max=ave;
printf("The highest average is %.2f", max);
getch();
}
I hope you can help me, and to all there who is willing to help.. I will appreciate it. and thank you!
#include <iostream>
#include <string>
usingnamespace std;
#define NUM_OF_STUDENTS 10 //define how many students are there
struct StudentStruct //A student struct to store data
{
std::string name;
float score[5];
float average_score;
};
float GetAvgScore(StudentStruct student) //Returns the average score of the student
{
float total_score = 0;
for(int a=0;a<5;a++)
total_score += student.score[a];
return total_score*0.2f; //divide by 5
}
void main()
{
StudentStruct Students[NUM_OF_STUDENTS];
//Get Student Input
for(int i=0;i<NUM_OF_STUDENTS;i++)
{
printf("Enter name %d : \n", i+1); //i+1 offset because loop starts with 0
cin >> Students[i].name; //Get Name
//Get Individual Scores
for(int j=0; j<5; j++)
{
printf("Enter score %d :", j+1); //j+1 offset because loop starts with 0
cin>>Students[i].score[j]; //Get the 5 Scores
}
//Calculate Average Score and stores it
Students[i].average_score = GetAvgScore(Students[i]); //Get Average
printf("The average score is %.2f \n", Students[i].average_score);
}
//Find out who has the best result by comparing average score
float highest_score = -1.f; //Set a negative value
unsignedint best_student = 0; //ID of best student
for(int k=0;k<NUM_OF_STUDENTS;k++)
{
if(Students[k].average_score > highest_score) //see if current student beats the highest score
{
highest_score = Students[k].average_score; //set the current high score
best_student = k; //best student ID
}
}
printf("The best student is %s. \n", Students[best_student].name.c_str()); //prints out the best student name
system("pause"); //pause the program
exit(0); //exit the program with code 0
}
Thank you Ultima for your advice, it helps me a lot especially when you instructed me to reset my sum to 0 before my second loop, now my problem is how do i make a first name that has a family name?, i mean when i enter a name with space the program will not input a name anymore in the second loop, for example when i enter the name of "David Smith ", the program will not ask for a second name and lastly when i computed the average, it does not give me the exact decimal for example when i input a score of : 90 + 90 + 90 + 60.5 + 80.4 divided by 5, the program will output 82.00 which is wrong instead of 82.12 which is correct.
here is the code :
#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
char name;
float score;
int sum=0;
float ave=0,max=0;
for(x=1;x<=4;x++)
{
printf("Enter name %d : \n", x);
scanf("%s", &name);
sum = 0;
for(y=1;y<=5;y++)
{
printf("Enter score %d :", y);
scanf("%f", &score);
sum=sum+score;
}
ave=(sum/5);
printf("The average score is %.2f \n", ave);
}
if(ave>max)
max=ave;
printf("The highest average is %.2f", max);
getch();
}
I hope you can help me for the last time. And to all out there who is willing to help. Thank you!
Sorry for the late reply, was busy over the past week.
1) Problems with ur name:
- Your name is char type. Which means it doesn't store %s (string) correctly. It only stores the first letter if i'm not wrong. Try using char* or std::string instead.
- scanf() and cin breaks at special characters such as space, which means that ur string of "david smith" will break at david and smith, individually spilt into 2 components. For that, you have to use getline() function to get the whole input, including the space.
2) Average score is still incorrect.
- Your sum has type of integer. Change it to float.
- Another precaution to take note of: Every time you calculate the floats such as avg=(sum/5), try putting avg=sum*0.2f instead. This step ensures that you are handling floating points instead of dividing it by an integer 5. So you might lose the decimal points in the process of calculating it.