Noob Question

This is a discussion on Noob Question within the C Programming forums, part of the General Programming Boards category; Hi All, I am a complete Noob to C, this being my first day of what i hope will be ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Question Noob Question

    Hi All,

    I am a complete Noob to C, this being my first day of what i hope will be a long,wonderful adventure .

    I have come up with a very simple program, that just asks some very basic questions of the user and then goes down different interaction tracks based on said user input.

    My question is, can someone give me some pointers on best practice - I would prefer a seasoned veteran to give me a going over at this early stage and point me in the right direction so that i don't bed in with any bad habits.

    Anyones time on this would be greatly appreciated by myself :

    Code:
    #include <stdio.h>
    
    
    
    
    
    
    int main(void)
    {
    
     int numneed,income,outgoing;
     char shallwe,fname[30];
    
    
    fputs("Whats your name ?\n",stdout);
     fflush(stdout);
     fgets(fname,sizeof(fname),stdin);
    printf("Nice to meet you %s\n",fname);
    
    
    
    printf("Hi, How much do you need this month to afford a new macbook ()? : \n" );
    
     scanf("%d", &numneed);
     getchar();
     if ( numneed > 1500)
    printf("WOW That is a lot of cash needed ! \n");
     else
    printf("Its alot, but might be 'do able' \n");
    
    
    printf("Well, let's work it out now shall we ? Y or N ? \n");
    
     scanf("%s",&shallwe);
     getchar(); 
    
     if (shallwe == 'y')
            {printf("Ok, i will jot down a few figures and we can go from their! \n");
    printf("How much will you earn this month () ?\n");
     scanf( "%d",&income);
    printf("Not bad, but what about outgoings ? \n");
     scanf( "%d",&outgoing);
    
     int left = income - outgoing;
    
     if ( left >= 0)
    printf("Well you do have %d left over, so its not too bad! \n" ,left);
    elseprintf("Not good, your in the red! \n");}
    
    elseprintf("Ok well good luck !");
    
    
     return 0;
    }
    
    

    Kind Regards,
    Dan

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,692
    One of the first things I will recommend is that you find an indention style you like and use it consistently. This will make you program much easier to read and troubleshoot.

    Next you need to study the documentation for the standard functions you want to use to insure you are properly using them. For example the following snippet:
    Code:
       char shallwe,fname[30];
    
    .....
       scanf("%s",&shallwe);
    The "%s" format specifier for scanf() is used to retrieve a C-string from the console. However you are trying to retrieve a single character, not a C-string. The proper format specifier for a character is "%c".

    Also in future please post your code as plain text.

    Jim

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I am not a veteran but i want to answer.If a veteran question is a must,then just skip my answer

    What i would suggest is to

    • [code]fputs("Whats your name ?\n",stdout);
      fflush(stdout);
      Code:
      should be replaced by printf("Whats your name ?\n")
      which actually sends the data by default to the stdout and then there is no need for fflush at the next line(if you use printf).
    • The variable numneed,because it represents a price might better not be an integer but a double.Many products may costs for example 99.99 euro.If you use double variable then you should %f instead of %d
    • Here
      Code:
       scanf("%s",&shallwe);
      you read s single character so why do not using %c instead of %s ?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,219
    Code:
    printf("Well, let's work it out now shall we ? Y or N ? \n");
    
    scanf("%s",&shallwe);
    getchar(); 
    
    if (shallwe == 'y')
    // ...
    It has already pointed out that you should use "%c" in your "scanf()" to retrieve a character. In addition, the "if()" statement will only execute if the user enters a lowercase 'y'. If you wanted it to accept either case (upper or lower), you would have to code it to do so.

    One approach would be to test for both conditions using the logical "or" operator:

    Code:
    if(shallwe == 'y' || shallwe == 'Y')
    Another would be to use a function such as "tolower()" to turn the character into lowercase before the "if()" statement in the event it was entered as uppercase.
    Last edited by Matticus; 08-21-2012 at 03:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another noob question
    By Gikimish in forum C Programming
    Replies: 11
    Last Post: 10-02-2010, 05:14 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 12:28 PM
  3. NOOB Question!
    By Paul22000 in forum C Programming
    Replies: 4
    Last Post: 04-23-2008, 11:58 AM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM

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