Code is hanging - Very simple C (Beginner)

This is a discussion on Code is hanging - Very simple C (Beginner) within the C Programming forums, part of the General Programming Boards category; Hi, Im trying to get this program to run - it complies but it crashes in DOS. It is down ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    20

    Code is hanging - Very simple C (Beginner)

    Hi,

    Im trying to get this program to run - it complies but it crashes in DOS. It is down to the Stuname i think, or maybe having two print F's. Hope you can help.

    Jon

    Code:
    #include <stdio.h>
    
    main()
    
    {
          int stuname, mathsmark, englishmark, overallgrade;
          
          /*get marks */
          
          printf("What is your name\n");
          scanf("%s", stuname);
          
          printf("Please key in your maths mark\n");
          scanf("%d", &mathsmark);
          
          printf("Please key in your english mark\n");
          scanf("%d", &englishmark);
          
          
          /*Calculate total mark*/
          overallgrade = mathsmark + englishmark;
    
          /*display total mark*/
    
          printf("your total mark", stuname);
          printf("is %d", overallgrade);
    
          return 0;
    }

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    printf("your total mark", stuname);

    it's missing something

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    20
    Quote Originally Posted by indigo0086 View Post
    printf("your total mark", stuname);

    it's missing something

    Is this it? As it didn't work with this as well.

    Code:
    printf("your total mark%s", stuname);

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("What is your name\n");
    > scanf("%s", stuname);
    stuname should be declared as a char array. And you might consider using fgets() instead of scanf() for reading it.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    int stuname, mathsmark, englishmark, overallgrade;
          
    /*get marks */
          
    printf("What is your name\n");
    scanf("&#37;s", stuname);
    WTF
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    20
    New code:
    But still crashing after entering a name(stuname).
    Code:
    #include <stdio.h>
    
    main()
    
    {
          int mathsmark, englishmark, overallgrade;
          char stuname[50];
          
          /*get marks */
          
          printf("What is your name\n");
          scanf("&#37;s", stuname);
          
          printf("Please key in your maths mark\n");
          scanf("%d", &mathsmark);
          
          printf("Please key in your english mark\n");
          scanf("%d", &englishmark);
          
          
          /*Calculate total mark*/
          overallgrade = mathsmark + englishmark;
    
          /*display total mark*/
    
          printf("your total mark %s", stuname);
          printf("is %d", overallgrade);
    
          system("PAUSE");
          
          return 0;
    }
    Last edited by pc_doctor; 10-29-2007 at 02:50 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    scanf() stops when it sees whitespace, so if the name entered contains spaces, you'll have to either use two scanf()'s, or use fgets() to read a line of text.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >scanf() stops when it sees whitespace
    Actually, scanf() ignores whitespace.

    As swoopy said, use fgets().

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    20
    It works now - It was because Vista must have been blocking something, because it works in XP now.
    What programs etc do you use for C programming?

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by zacs7 View Post
    >scanf() stops when it sees whitespace
    Actually, scanf() ignores whitespace.
    Well, it ignores it by "stopping," i.e. finishing the current conversion. Hitting whitespace doesn't stop the entire call to scanf() though.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >What programs etc do you use for C programming?
    A text-editor (SciTE), and a compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 12:31 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 10:34 AM

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