5 Replies - 667 Views - Last Post: 09 May 2011 - 03:10 PM Rate Topic: -----

#1 tornadoeddie   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-February 11

Repeats name and exam scores on same line

Posted 09 May 2011 - 05:21 AM

When I run this program it asks you to enter the student name which is correct but then when it asks for the exam scores, it asks for the exam scores, student name and exam scores again on the same line. Can someone help me out with this please.





 #include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int i;
  typedef struct student_t {
   char name[19];
   double exam1;
   double exam2;
   double exam3;
   double avg;
     } student;
     
   struct student_t s[50];
   int counter=0;
   char temp[19];
   double classAvg=0.0;
   
   while(strcmp(temp,"0")!=0)
   {
   printf("Enter student name (0 to stop): ");
   scanf("%s",&temp);
   if(strcmp(temp,"0")!=0)
   {
   strcpy(s[counter].name,temp);
   printf("Enter student's three exam: ");
   scanf("%lf",&s[counter].exam1);
   scanf("%lf",&s[counter].exam2);
   scanf("%lf",&s[counter].exam3);
   counter++;
   }
   
   }
   
   for(i=0;i<counter;i++)
   {
       double avg;
       avg=(s[i].exam1+s[i].exam2+s[i].exam3)/3;
       s[i].avg=avg; 
       classAvg+=avg;   
   }
   
   printf("Name               Exam 1      Exam 2     Exam 3      Average \n");
   for(i=0;i<counter;i++)
   {
      printf("%s               %0.2lf        %0.2lf        %0.2lf        %0.2lf\n\n",s[i].name,s[i].exam1,s[i].exam2,s[i].exam3,s[i].avg);
      
   }
   printf("\n\n The class average is: %0.2lf\n",classAvg);
   
  system("PAUSE");	
  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Repeats name and exam scores on same line

#2 jimblumberg   User is offline

  • member icon

Reputation: 5567
  • View blog
  • Posts: 17,213
  • Joined: 25-December 09

Re: Repeats name and exam scores on same line

Posted 09 May 2011 - 05:36 AM

First you need to check your compiler settings and insure it is emitting warnings. Compiling with just errors reported will not produce reliable code. When I compile your code I get the following warnings:

Quote

main.c||In function ‘main’:|
main.c|20|warning: implicit declaration of function ‘strcmp’|
main.c|23|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[19]’|
main.c|26|warning: implicit declaration of function ‘strcpy’|
main.c|26|warning: incompatible implicit declaration of built-in function ‘strcpy’|
main.c|4|warning: unused parameter ‘argc’|
main.c|4|warning: unused parameter ‘argv’|
||=== Build finished: 0 errors, 6 warnings ===|


You should first fix these warnings and see if it fixes your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 tornadoeddie   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-February 11

Re: Repeats name and exam scores on same line

Posted 09 May 2011 - 05:37 AM

ok thanks I will try this first.
Was This Post Helpful? 0
  • +
  • -

#4 tornadoeddie   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-February 11

Re: Repeats name and exam scores on same line

Posted 09 May 2011 - 02:11 PM

View Posttornadoeddie, on 09 May 2011 - 05:37 AM, said:

ok thanks I will try this first.


I do not get any warnings. If I run the program and just add a first name and not a last the program works fine but if I add the last name after the first it does not work properly.
Was This Post Helpful? 0
  • +
  • -

#5 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Repeats name and exam scores on same line

Posted 09 May 2011 - 02:19 PM

#include <string.h>

simple?

scanf("%s",&temp);

'temp' is already an array and passed as a pointer. it's undefined behavior to pass a pointer to a pointer to scanf, it needs to be fixed.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5567
  • View blog
  • Posts: 17,213
  • Joined: 25-December 09

Re: Repeats name and exam scores on same line

Posted 09 May 2011 - 03:10 PM

Quote

I do not get any warnings. If I run the program and just add a first name and not a last the program works fine but if I add the last name after the first it does not work properly.


If you have spaces in your input string then you must use fgets not scanf(). The scanf() function stops processing when it encounters white space.

Also be sure to include the string.h header file when you use the string functions like strcmp(), strcpy(), and strcat().

Jim

This post has been edited by jimblumberg: 09 May 2011 - 03:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1