Thread: Am I doing something wrong?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    40

    Am I doing something wrong?

    Hi,

    I've learned structures in class, and I tried out an example given by the teacher. I'm using MS Visual Studio C++ 2008 express. I think I have the code he gave written correctly.

    Here's the code, you enter in student's first, last name, three test scores and avg is calculated. It's using a structure and some functions. I think it should work, but when I try running the program, there's no compiling error, but command prompt always ends up with an error box, saying the exe crashed or something like that and program need to be closed. Did I do something wrong?? THANKS!!

    Code:
    #include<stdio.h>
    #define SIZE 10
    
    struct Student
    {
    	char last[15], first [10];
    	int t1, t2, t3;
    	float avg;
    };
    
    void load(struct Student s[], int n)
    {
    	for(int i=0; i<n; i++)
    	{
    		printf("Enter last name: ");
    		gets(s[i].last);
    
    		printf("Enter first name: ");
    		gets(s[i].first);
    
    		printf("Enter 3 scores: ");
    		scanf("%d%d%d", s[i].t1, s[i].t2, s[i].t3);
    
    		s[i].avg = (s[i].t1 + s[i].t2 + s[i].t3) / (float)3;
    
    		fflush(stdin);
    	}
    }
    
    void print(struct Student s[], int n)
    {
    	for(int i=0; i<n; i++)
    	{
    		printf("%s %s\n", s[i].first, s[i].last);
    		printf("Test scores are: %d %d %d\n", s[i].t1, s[i].t2, s[i].t3);
    		printf("Avg is: %f\n\n\n", s[i].avg);
    	}
    }
    
    void main()
    {
    	Student s[SIZE];
    	load(s, SIZE);
    	print(s, SIZE);
    }
    Last edited by x2x3i5x; 04-27-2010 at 04:40 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need pointers (an address) when using the *scanf functions, not just a value:
    Code:
    scanf("%d %d %d", &s[i].t1 ... )
    Also, you probably want spaces in there so you can separate your numbers. Otherwise your input will have to be something weird like:

    123234231 ... and where is it supposed to know to split that into three numbers?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Quote Originally Posted by quzah View Post
    You need pointers (an address) when using the *scanf functions, not just a value:
    Code:
    scanf("%d %d %d", &s[i].t1 ... )
    Also, you probably want spaces in there so you can separate your numbers. Otherwise your input will have to be something weird like:

    123234231 ... and where is it supposed to know to split that into three numbers?


    Quzah.
    Thank you very much, a simple error that I did not catch.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    how to check if text file is blank
    Where you used int main and fgets(), which is good.

    > I think I have the code he gave written correctly.
    Um, void main, gets() and fflush(stdin)
    An unholy trinity of disaster.
    You're going in the wrong direction!

    > Am I doing something wrong?
    Remaining in that class perhaps?
    It's doubtful that you will learn anything of lasting usefulness from some bozo stuck with TurboC as their reference compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    I don't think it a problem, if you use the stuff correctly.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by x2x3i5x View Post
    I don't think it a problem, if you use the stuff correctly.
    It is not possible to use correctly that which is fundamentally wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM