help

Started by
2 comments, last by Lt Drao 20 years, 11 months ago
I was doing a algorythm to exercise what I´ve learned so far, the first function works, but the second, when I want to multiply the 2 numbers the result is not right. Here is the code: #include <cstdlib> #include <stdio.h> int function1() { for(int i = 0;i <= 3; i++) { int what; scanf("%d", &what); switch(what) { case 1: printf("Number one!\n"); break; case 2: printf("Number two!\n"); break; case 3: printf("Number three!\n"); break; default: printf("Other number!\n"); break; } } return 0; } int function2() { int a; int b; int c = a * b; scanf("%d", &a ); scanf("%d", &b); printf(" %d * %d = %d", a , b , c); return 0; } int main () { int e; scanf("%d", &e); if (e == 1) function1(); if (e == 2) function2 (); system("pause"); return 0; }
Advertisement
I suppose you are talking about this:

int a;
int b;
int c = a * b;

Well... You can''t multiply 2 variables that don''t have any value. You need to initialize them to something before using them. For example, this would work:

int a = 5;
int b = 2;
int c = a * b;
-----DevZing Blog (in Spanish)
quote:Original post by Lt Drao
int function2()
{
int a;
int b;
int c = a * b;
scanf("%d", &a );
scanf("%d", &b);
printf(" %d * %d = %d", a , b , c);
return 0;
}


You''re confusing algebra variables with programing variables here. Conceptually, statements are evaluated in the order they are written. Due to this, the line "int c = a * b;" makes no sense because a and b are uninitialized values. You want to get the input for a and b before writing that statement, like so:

  int function2()   {     int a;     int b;     scanf("%d", &a  );     scanf("%d", &b);     int c = a * b;     printf(" %d * %d = %d", a , b , c);     return 0;   }




______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
int function2(){int a;int b; 

Ok, you''ve declared these variables that you know you''ll be using very shortly in this function. You haven''t initialized them to a value, which means you''re relying on the fact that later you WILL be assigning them values. Until you do that, you can''t trust what values they''ll contain. (Actually, there are some rules about initial values of variables declared on the stack as you''ve done here, but it''s best just assume that these variables have no value that you can trust. yet).
int c = a * b; 

Whoops. You multiplied too early. You can''t say for sure what A and B are yet, so C is certainly suspect. And the following two lines are where you read in A and B, so you''re better off putting this line below...
scanf("%d", &a );scanf("%d", &b); 

This is where you should be multiplying A and B. A and B have been read in from the user, and it''s safe to do your math.
printf(" %d * %d = %d", a , b , c); 

(BTW, you could also do: printf(" %d * %d = %d", a, b, a*b);
That would be a simplification. You''d have to consider if that made the code more or less readable. Probably best NOT to put something like a multiplication in a printf statement. There''s a "business rule" of sorts when you mulitply A and B, and it''s best to leave that out in the open, like you did.
return 0;} 


So you just need to move your c = a * b; line down 2...

Mark

This topic is closed to new replies.

Advertisement