0
Go4Expert Member
alright. Well basically it is trying to add each number, regardless of what is put in. and even when you tell it to add, it isn't using the numbers you put it in. it seems to add them and use that as a, then add the second value you put in again, and set it to what seems a random number. For example I put in 4 + 6 and it spit out an answer of 10 + 6 = 43. thats just the adding, figured I'd look at one at a time.
0
Go4Expert Member
can you post the changed code?
0
Go4Expert Member
#include <stdio.h>

int main()
{
int a;
scanf("%d", &a);
{
char b;
scanf("%c", &b);
if (b=='+');
{ int c, d;
scanf("%d", &c);
d = a + c;
printf("%d + %d = %d"), a, c, d;
return 0;
}
if (b=='-')
{
int c, d;
scanf("%d", &c);
d = a - c;
printf("%d - %d = %d"), a, c, d;
return 0;
}
if (b=='*')
{
int c, d;
scanf("%d", &c);
d = a * c;
printf("%d * %d = %d"), a, c, d;
return 0;
}
if (b=='/')
{
int c, d;
scanf("%d", &c);
d = a / c;
printf("%d / %d = %d"), a, c, d;
return 0;
}
}
}

That's the changed code, I just took out the loop for the moment being. The only thing I can think might be causing the problem is that I declared integer a before the bracket containing c and d. But I don't think that would cause this.
0
Go4Expert Member
so...i played with your code a bit, first off....after the if statement you dont' want that ";" because it ends the if and will always run the stuff in the brackets. secondly your printf statement printf("%d + %d = %d"), a, c, d; should look more like printf("stuff %<var type>" , variable) so the () wrap the whole thing. and lastly when running your code i found that int a and char b have the same memory address for some reason....when i move char up it runs...so id suggest for future when you try to debug printf everythign you can around teh problem...im attaching the code that works so you can see what i did..i also changed teh b to being %d and giving it the ascii value of 43, this is just because iw as testing the if statement at first and didn't notice the ";" and i didn't go back and change it

[code]char b;
int a;
printf("what is a: ");
scanf("%d", &a);
printf("you entered a as: %d\n", a);
printf("a is still %d\n", a);
printf("address of a: %d\naddress of b: %d\n", &a, &b);
printf("what is b: ");
//printf("test: %d\n", a);
scanf("%d", &b);
printf("a: %d\n", a);
printf("the value of b is: %c\n", b);
printf("a before if statement %d\n", a);
if (b=='+')
{
printf("a after if statement %d\n", a);
int c, d;
printf("what is c: ");
scanf("%d", &c);
printf("the value of c is: %d\n", c);
printf("about to add %d = %d + %d\n", d,a,c);
d = a + c;
printf("%d + %d = %d", a, c, d);
return 0;
}[\code]
0
Go4Expert Member
I made the changes you mentioned and it works fine now. I have it built and compiled. Thank you SO much for the help
0
Mentor
Swapping variables around to solve an odd memory issue is never the correct approach, find and solve the real problem!

I'm not clear what the problem is in the above code, I'm using Visual Studio 2005 and it works perfectly. However I'm using scanf("%c",&b) for reading into b, not %d, because b contains the operator. which is a single character (one byte). That's likely the problem.

Essentially if you do char b; scanf("%d",&b); you have a memory corruption; scanf reads the input and writes it as an integer (4 bytes) to the address where b is stored (where you only allocated one byte). Silcrome, if you swap a and b back and use %c instead of %d, does that solve the problem?
I checked with Visual Studio doing char b; scanf("%d",&b); and threw a debug error: stack around the variable 'b' was corrupted.

Ah, yes, just read your post a bit more accurately. "i also changed teh b to being %d and giving it the ascii value of 43" So you're allocating ONE BYTE for storage of a FOUR BYTE integer. Yes, this is definitely a memory corruption.

So here's my code:
Code:
```void add_test1()
{
int a;
scanf("%d", &a);
printf("You entered '%d'\n",a);

char b;
scanf("%c", &b);
printf("You entered '%c'\n",b);
if (b=='+')
{
int c, d;
scanf("%d", &c);
printf("You entered '%d'\n",c);
d = a + c;
printf("%d + %d = %d\n", a, c, d);
}
}```
and the input (first line, entering 5+7 because if I entered 5 then pressed return, the scanf...&b read the end of line as the next character. This is partly why scanf is a terrible function.) and output:

5+7
You entered '5'
You entered '+'
You entered '7'
5 + 7 = 12
0
Go4Expert Member
i understand that changing to %d wasn't a great idea for a final solution... but i don't use scanf much and didn't understand why no matter what i was giving it was running teh inside of the if statement...once i noticed that it had a semicolon i just didn't change it back, i thought i had specified that in my post but maybe it wasn't clear that a correct solution wouldn't have that "hack" in it
0
Go4Expert Member
one last question, though it isn't NEEDED for the code. is there a command or function the will skip a line with printf, beacuase as it is the program will display the answer, then I want it to skip a line and ask if the user is finished. Ihave the code wirtten so that it asks, but it is RIGHT after the problem, same line and no space
(sorry if some of my terms are incorrect)
0
Mentor
Embed \n in the code; it prints a newline. For example printf("Hello\nWorld\n") will print
Hello
World
<-- the next printf will print from here
0
Go4Expert Member
Well, I dunno if this will be of any use to you guys, but here is the finished code. I have compiled it and everything, it works great.
Code:
```#include <stdio.h>

int main()
{
int z;
z = 0;
while (z < 1)
{
int a;
scanf("%d", &a);
{
char b;
scanf("%c", &b);
if (b=='+')
{
int c, d;
char e;
scanf("%d", &c);
d = a + c;
printf("%d + %d = %d\n", a, c, d);
printf("Are you finished?");
scanf("%c", &e);
if (e=='y')
{
return 0;
}
}
if (b=='-')
{
int c, d;
char e;
scanf("%d", &c);
d = a - c;
printf("%d - %d = %d\n", a, c, d);
printf("Are you finished?");
scanf("%c", &e);
if (e=='y')
{
return 0;
}
}
if (b=='*')
{
int c, d;
char e;
scanf("%d", &c);
d = a * c;
printf("%d * %d = %d\n", a, c, d);
printf("Are you finished?");
scanf("%c", &e);
if (e=='y')
{
return 0;
}
}
if (b=='/')
{
int c, d;
char e;
scanf("%d", &c);
d = a / c;
printf("%d / %d = %d\n", a, c, d);
printf("Are you finished?");
scanf("%c", &e);
if (e=='y')
{
return 0;
}
}
}
}
}```

Last edited by shabbir; 2Aug2008 at 10:57.. Reason: Code block