PDA

View Full Version : Programmers, help



MalikT
2009-06-27, 09:16 AM
I have a assignment for my programming class. A simple program in c++. It's been a long time since I programmed in c++, but I thought I could handle it. It's a really simple program: output should be 1 or 0 based on the values of three variables. Here is my code:


int a,b,c,q;
printf ("Unesite varijablu a: \n");
scanf ("%d", &a);
printf ("Unesite varijablu b: \n");
scanf ("%d", &b);
printf ("Unesite varijablu c: \n");
scanf ("%d", &c);
if (a%2==0&&a>b||c<0||c>100) q=1;
else q=0;
printf ("%d \n"), q;


It prints out q=2293580. Where did I go wrong.

Thajocoth
2009-06-27, 09:25 AM
if (a%2==0&&a>b||c<0||c>100) q=1;
else q=0;

It's easier to read with more parentheses, and this also ensures the operations run in the right order:

if (((a % 2) == 0) && (a > b) ||(c < 0) || (c > 100))
{
q = 1;
}
else
{
q = 0;
}


The problem, however, is with the parentheses placement here:

printf ("%d \n"), q;

Specifically, by being outside the parentheses, printf is grabbing garbage from a random memory location. Really, I'm not sure why this compiled with ", q" between a function and a semicolon.

St.Sinner
2009-06-27, 09:27 AM
The q in your printf line should be inside the parenthesis.

printf ("%d \n", q);

MalikT
2009-06-27, 11:00 AM
Thanks, I'm not sure why was q out of parentheses. It seems I became a bit rusty.

darkjubs
2009-06-27, 01:53 PM
Just out of curiosity, since you're using c++ why did you choose printf and scanf over cout and cin?

For example,

std::cin >> a;
...
std::cout << q << std::endl;

Pyrian
2009-06-27, 02:07 PM
Really, I'm not sure why this compiled with ", q" between a function and a semicolon.It's amazing what can turn out to be valid code in C++. :smallcool:

http://en.wikipedia.org/wiki/Comma_operator

Thajocoth
2009-06-27, 03:29 PM
Just out of curiosity, since you're using c++ why did you choose printf and scanf over cout and cin?

For example,

std::cin >> a;
...
std::cout << q << std::endl;

printf is a little more flexible & easier to format. (And you don't have to remember which arrows are for which command.)


It's amazing what can turn out to be valid code in C++. :smallcool:

http://en.wikipedia.org/wiki/Comma_operator

Looks great for Obfuscated Code contests. I could see some very very rare non-obfuscated uses for it, though...

Tirian
2009-06-28, 06:53 PM
printf is a little more flexible & easier to format. (And you don't have to remember which arrows are for which command.)

Ugh, I always get that wrong too. If it was supposed to be intuitive to people who grew up with DOS and Unix command line redirection, then it sure failed in my case. Plus it only takes one time of trying to chase down the syntax error of figuring out why cout >> x++ works but cout >> x += 2 doesn't before you decide that you'd rather steer away from solutions that overload the bitshift operator. Also, if the UVa Online Judge taught me anything, it's that iostream is often much slower than stdio. I'm not saying that iostream doesn't have its place, but I say that there's no reason to fight the instinct to use scanf and printf if that's what comes naturally to you.