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 ...

Noob Question

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");elseprintf("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 !");return0;}

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".

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

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.