-
Stupid question
Well they say that there is no such thing as a stupid question...well here I am:(
Alright I am learning and reading a book and they were talking abut constants so I decided that it would be interesting to go and check them out and try them for myself, so this is what I came up with:
Code:
#include <stdio.h>
unsigned int positive;
int posAndneg;
const double decimal1 = 1.123321;
#define decimal2 = 2.234432;
double userTotal = positive + posAndneg;
double userTotalF = userTotal + decimal1 + decimal2; /*First time error occoured*/
main(void)
{
printf("Please enter a positive number: ");
scanf("positive");
printf("Please enter another number(either positive or negative)");
scanf("posAndneg");
printf("The total of adding both of your numbers together is: %f", userTotal);
printf("The computer will now add %d and %d to the total of your two numbers", decimal1, decimal2); /*Second time error*/
printf("The final total is: %d", userTotalF);
return 0;
}
Now I have made a few small programs to play with things and have managed to fix all of those errors, with some help from the board of course, but this time I am truly lost, here are the errors:
error C2059: syntax error : '='
Now I get this one but it appears twice, it appears the first time on the line that has the comment "first time" on it and this I think is just me not knowing about the different types of variables and such so that should be easy for someone who knows.
Then it appears again on the line that has "second time" on it and I don't get that one at all because I can't seem to find the '=' in it.
And finally
syntax error : missing ';' before ')'
I know what this means and how to fix it but I have no idea where to put the ; , the error appears on the same line as 'Second time'
If someone can help give a bit of a lesson I would be grateful.
-
Change this
>#define decimal2 = 2.234432;
to this
>#define decimal2 2.234432
Also, my compiler moans about
>double userTotal = positive + posAndneg;
You need to declare the variables, then, within main(), once you have the users input, apply values to them.
-
>scanf("positive");
>scanf("posAndneg");
is wrong
You have some thing called the format specifiers, basically telling scanf what kind of data to read. In your case, it would be:
Code:
scanf("%u", &positive);
scanf("%d", &posAndneg);
-
Is there a place I can know more about those specifiers? I know some of them but not all.
-
This is just one I found out of many here
-
-
while defining the constant variable using define.
dont use "="
ex: #define decimal1 1.2222
-
Alright I tried the things that you said and I came up with more problems.
#1. When I tried to change scanf("posititive") to scanf("%u" &positive) it came up with this:
'&' : illegal, left operand has type 'char [3]'
Is this because of the & sign or did I just type something wrong?
Also I am still getting those syntax errors but I can't seem to find the problem, here is what they are:
syntax error : missing ')' before ';'
and
syntax error : ')'
I don't know a whole lot but I do know that this means I should just insert a closing bracket in there right?
Anyways here is my code again(with some changes)
Code:
#include <stdio.h>
unsigned int positive;
int posAndneg;
const double decimal1 = 1.123321;
#define decimal2 2.234432;
double userTotal; /*positive + posAndneg;*/
double userTotalF; /*userTotal + decimal1 + decimal2;*/
main(void)
{
printf("Please enter a positive number: ");
scanf("%u" &positive);
printf("Please enter another number(either positive or negative)");
scanf("%d" &posAndneg);
userTotal = positive + posAndneg;
printf("The total of adding both of your numbers together is: %f", userTotal);
printf("The computer will now add %d and %d to the total of your two numbers", decimal1, decimal2);
userTotalF = userTotal + decimal1 + decimal2;
printf("The final total is: %d", userTotalF);
return 0;
}
Thanks again
-
Code:
#include <stdio.h>
unsigned int positive;
int posAndneg;
const double decimal1 = 1.123321;
#define decimal2 2.234432/* ; */
double userTotal; /*positive + posAndneg;*/
double userTotalF; /*userTotal + decimal1 + decimal2;*/
int main(void)
{
printf("Please enter a positive number: ");
scanf("%u", &positive);
printf("Please enter another number(either positive or negative)");
scanf("%d", &posAndneg);
userTotal = (double)positive + (double)posAndneg;
printf("The total of adding both of your numbers together is: %f", userTotal);
printf("The computer will now add %f and %f to the total of your two numbers", decimal1, decimal2);
userTotalF = userTotal + decimal1 + decimal2;
printf("The final total is: %f", userTotalF);
return 0;
}
-
Thanks I will try that, when I get a chance.
-
And when you get the hang of it, try checking the return code from scanf() to ensure the user has entered a valid number.
-
Of course, I am just going to make it and then keep modifying it, simple -yes- but I think it is worthwhile to keep trying anyways, thanks for all the help.
-
>thanks for all the help.
No problem.... :)