PDA

View Full Version : Quetion about C programing



Wolfar777
04-11-2005, 01:11 PM
Ok what the heck am I doing wrong here. I need for the users input to show up not the word user. I also need for it to calculate correctly as it does right now upto 12.

Thank you.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int multiplier, user, g;

printf ["Please input a number to multiply:" ];
scanf ["%d", &user];
printf ["\n"];

for [multiplier = 1; multiplier <= 12; multiplier++]
printf["user * %2d = %2d\n", multiplier, multiplier * user];
system ["pause"];
return 0;


}

Thank you!

noshens
04-11-2005, 01:54 PM
wow didn't know C++ is so different

Bubblehead1948
04-12-2005, 07:59 AM
http://forums.ubi.com/groupee_common/emoticons/icon_smile.gif Look at the following statement.

printf["user * %2d = %2d\n", multiplier, multiplier * user];

The quote marks are what is causing the literal printing of user instead of the interger value of user. I don't think they should be there.

radsov
04-12-2005, 08:33 AM
No he needs the quotes. C can be a strange syntax. It's been well over ten years since I did any C programming. However looking at your code the main error you've made is to miss out your user number variable place holder. I'm guessing you want the output to read something like (presuming user inputs number 9)

9 * 1 = 9
9 * 2 = 18

etc. etc.

To get this the printf statment should be;

printf("%2d * %2d = %2d\n", user,multiplier, multiplier * user);

Bubblehead1948
04-12-2005, 06:30 PM
http://forums.ubi.com/groupee_common/emoticons/icon_smile.gif Hmmm... I'm still baffled by the neccessity of the quotes. Oh well...

radsov
04-13-2005, 02:41 AM
I understand, even if you programmed in other languages C can have a bit of an odd looking syntax, particulary if you've only used VB or Pascal based languages. Basically the syntax of the printf statement is;

printf(String,{any number of variables})

so if you just wrote

printf("Hello World");

It would just print that. The first parameter of printf is always a string, hence you need the quotes. If you want to print a value of a variable, for example what is stored in 'user' you need to put in special place holders indicating to C where you wish the value to print, so to print out the value of user you could write;

printf("The value of user is : %i",user);

The percentage sign indicates that you wish to insert a variable value at this point. The i indicates it will be a integer number. The actual %i will not print out, it will be replaced by the value of the first parameter passed after the string, in this case user. So if user had the value 12 then it would print out

The value of user is : 12

If you wanted to print something like "The value of number x number is number" then the following line would do the trick

printf("The value of %i x %i is %i",user,user,user * user);

For each %i C comes across it looks at the corrosponding variable after the string. So for the first %i it looks at the first user then the second and then finally for the last %i in the string it evaluates the expression user * user.

Hope this makes it clearer for you about what is going on. Note that Wolfar777 uses a %2d, this just means format the number as a decimal to 2 decimal places.