Simple C Program

This is a discussion on Simple C Program within the C Programming forums, part of the General Programming Boards category; I'm a newbie, can someone help? First, I'm getting two errors I can't really find using my compiler. They read: ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    Simple C Program

    I'm a newbie, can someone help?
    First, I'm getting two errors I can't really find using my compiler.

    They read:
    -Local 'choice' is used but never assigned a value. (Line 24)
    -Syntax error; found 'end of input' expecting '}'. (Line 36)

    Second, on an alternate computer I used, I didn't get any errors but the program basically doesn't work. It outputs the name and reads everything correctly and then I am able to type in text that would be the actual grades. E.g., I just enter an A, B, C, and E to finish it and it just keeps entering a new line no matter what I type in. Can someone help? Thanks in advance.


    Code:
    #include <stdio.h>
    
    void main() {
    
    	char name[80];
    	float gpa;
    	char agrade;
    	float totalinput;
    
    	totalinput=0.00;
    	agrade=0;
    
    	printf("Enter your name: ");
    	scanf("%s", &name);
    	printf("\n\n");
    
    	printf("Hello %s.  This program is designed to calculate your GPA.\n", name);
    	printf("Type in your grades individually and hit enter to proceed to the next.\n");
    	printf("To calculate your grade, enter in 'E' as a grade.\n");
    	printf("Please commence entering grades now: \n");
    
    	while('agrade' != 'E') {
    
    		scanf("%e", &agrade); /* THIS IS MY LINE 24 */
    
    		if ('agrade' == 'A+') {
    			totalinput++;
    			agrade+=4.33;
    		}
    
    		if ('agrade' == 'A') {
    			totalinput++;
    			agrade+=4.00;
    		}
    
    		if ('agrade' == 'A-') { /* THIS IS MY LINE 36 */
    			totalinput++;
    			agrade+=3.77;
    		}
    
    		if ('agrade' == 'B+') {
    			totalinput++;
    			agrade += 3.33;
    		}
    
    		if ('agrade' == 'B') {
    			totalinput++;
    			agrade+=3.00;
    		}
    
    		if ('agrade' == 'B-') {
    			totalinput++;
    			agrade+=2.77;
    		}
    
    
    		if ('agrade' == 'C+') {
    			totalinput++;
    			agrade+=2.33;
    		}
    
    		if ('agrade' == 'C') {
    			totalinput++;
    			agrade+=2.00;
    		}
    
    		if ('agrade' == 'C-') {
    			totalinput++;
    			agrade+=1.77;
    		}
    
    
    		if ('agrade' == 'D+') {
    			totalinput++;
    			agrade+=1.33;
    		}
    
    		if ('agrade' == 'D') {
    			totalinput++;
    			agrade+=1.00;
    		}
    
    		if ('agrade' == 'D-') {
    			totalinput++;
    			agrade+=0.77;
    		}
    
    
    		if ('agrade' == 'F+') {
    			totalinput++;
    			agrade+=0.00;
    		}
    
    		if ('agrade' == 'F') {
    			totalinput++;
    			agrade+=0.00;
    		}
    
    		if ('agrade' == 'F-') {
    			totalinput++;
    			agrade+=0.00;
    		}
    
    	}
    
    		gpa=agrade/totalinput;
    
    		printf("Your GPA:  %f \n\n", gpa);
    
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main() {
    main returns int.

    >'agrade'
    That's not your variable, it's viewed as a character constant. You want to remove the single quotes.

    >'A+'
    That's not a legal character constant. If you want more than one character, you need to use a string instead, and that is a completely different solution.

    All in all, your code has issues. You might want to tone things down a little bit and play with a less complicated program until you're more comfortable with variables and constants.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    'foo' is actualy a valid character constant. The value of which is implementation defined (see multicharacter character constants). It certinaly is not what he wants here though.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
            char name[80];
    
        /* ... */
    
    	printf("Enter your name: ");
    	scanf("%s", &name);
    scanf() only reads one word at a time. So if you enter "Harry Hippopotamus" name will contain "Harry". To read in spaces, too, use fgets and remove the newline at the end (if you want to):

    Code:
        char name[80], *p;
    
        printf("Enter your name: ");
        fgets(name, sizeof(name), stdin);
        if(p=strchr(name, '\n')) *p = 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21