beginner. Need HELP!!!!

S

SKY[net]

Hi, I need some help. I'm a beginner programmer.

I need to do a comparison between 3 numbers and tell the user if it's
ascendinf, descending, equal or in no ordre. and ask if the user wants
to compare 3 more numbers. Here's what i've done.

that code only asks me the 3 numbers. It closes right after that. I
don't know if he does the comparison. Do you think I need a while to
ask the question if the user wants to compare 3 other numbers?


#include <stdio.h>

int num1, num2, num3;

void main ()

{

printf ("\n What are the 3 numbers to compare?");
scanf ("%s", &num1, &num2, &num3);

if (num1 < num2)
if (num2 < num3)
printf ("\n ascending");

if (num1 < num2)
if (num2 > num3)
printf ("\n no order");

if (num1 > num2)
if (num2 > num3)
printf ("\n descending");

if (num1 > num2)
if (num2 < num3)
printf ("\n no order");

if (num1 == num2)
if (num2 == num3) ¸
printf ("\n equals");

if (num1 == num2)
if (num2 != num3)
printf ("\n no order");

}
 
V

Victor Bazarov

SKY said:
Hi, I need some help. I'm a beginner programmer.
[..]
scanf ("%s", &num1, &num2, &num3);

You need to understand how 'scanf' works. Every variable you put
in the list _after_ the format, needs to have a corresponding field
in the format. In your case, if you're entering three _integers_,
you need to give three format fields: "%d %d %d".

And, in general, don't use 'scanf' unless you have to. Use 'cin'
and >>

std::cin >> num1 >> num2 >> num3;

No format to fix, no big deal. Don't forget to #include <iostream>

And find a good C++ book. Make sure it doesn't have 'void main'
in it. Writing 'void main' is embarrassing even for a beginner.

Victor
 
M

Mabden

SKY said:
Hi, I need some help. I'm a beginner programmer.

I need to do a comparison between 3 numbers and tell the user if it's
ascendinf, descending, equal or in no ordre. and ask if the user wants
to compare 3 more numbers. Here's what i've done.

that code only asks me the 3 numbers. It closes right after that. I
don't know if he does the comparison. Do you think I need a while to
ask the question if the user wants to compare 3 other numbers?

The code isn't very good, but your main problem is that you don't have a
loop. It will only ask again if you use some kind of looping like
"while". Then you will need a way for the user to stop the loop, so
something like "Enter X to exit" in the prompt (the printf) and a test
of the first input variable.

The reason it "closes right after that" is probably because you are
running the program without opening a command window (or DOS box). Try
opening a command window and running the program from the command line,
and you should see the output, and the box will stay open so you can run
it again.

I realize it must be frustrating to run into all these problem right
from the start, but keep at it and you'll get the hang of it.
 
D

DaKoadMunky

Writing 'void main' is embarrassing even for a beginner.

Of all the things one can know to make oneself a productive C++ programmer is
this really that important?

Sure, the smallest well-formed program looks like this...

int main()
{
return 0;
}

....but would you give somebody the thumbs down because they had been led to
believe that void was an acceptable return from main either because of an
incorrect textbook or a compiler that accepts it?
 
J

John Harrison

DaKoadMunky said:
Of all the things one can know to make oneself a productive C++ programmer
is
this really that important?

Sure, the smallest well-formed program looks like this...

int main()
{
return 0;
}

Gratuitous nitpick; the smallest well formed program is

int main()
{
}

Return is optional in main, even though int is compulsory. Crazy.

john
 
J

John Harrison

SKY said:
Hi, I need some help. I'm a beginne rprogrammer.

I need to do a comparison between 3 numbers and tell the user if it's
ascendinf, descending, equal or in no ordre. and ask if the user wants
to compare 3 more numbers. Here's what i've done.

that code only asks me the 3 numbers. It closes right after that. I
don't know if he does the comparison. Do you think I need a while to
ask the question if the user wants to compare 3 other numbers?


#include <stdio.h>

int num1, num2, num3;

void main ()

{

printf ("\n What are the 3 numbers to compare?");
scanf ("%s", &num1, &num2, &num3);

if (num1 < num2)
if (num2 < num3)
printf ("\n ascending");

if (num1 < num2)
if (num2 > num3)
printf ("\n no order");

if (num1 > num2)
if (num2 > num3)
printf ("\n descending");

if (num1 > num2)
if (num2 < num3)
printf ("\n no order");

if (num1 == num2)
if (num2 == num3) ¸
printf ("\n equals");

if (num1 == num2)
if (num2 != num3)
printf ("\n no order");

}

As well as the other problems (lack of while loop and incorrect scanf) your
logic could be improved. I don't think you have got all the possibilities,
suppose num1 < num2 but num2 == num3, then your program will print nothing.

The way to do this is with if ... else

if (numbers are ascending)
printf("ascending");
else if (numbers are descending)
printf("descending");
else
printf("no order");

See how simple that is. It simply cannot be wrong. Now all you have to do if
work out the correct expression for 'numbers are ascending' and 'numbers are
descending'. 'Numbers are ascending' is easy enough, just do this 'num1 <
num2 && num2 < num3'. You can work out the numbers are descending bit. Then
you will have a program that it simpler than your original, and more
correct.

It is very important to forget about a while loop for now, get it working
properly with one question first, then add the while loop later.

john
 
M

Mike Wahler

DaKoadMunky said:
Of all the things one can know to make oneself a productive C++ programmer is
this really that important?

Of course. A productive programmer is expected to write
programs that work correctly. A C++ program which defines
'main()' to return void, has absolutely no guarantee that
it will work correctly, or at all.

If I drive my car out of my driveway onto my (extremely
low traffic) street without pausing to look both ways,
chances are very good that nothing bad will happen.
But if I do look first, I am guaranteed the ability
to avoid driving into another car. I always look.
I always define 'main()' as returning 'int'.
Sure, the smallest well-formed program looks like this...

int main()
{
return 0;
}

...but would you give somebody the thumbs down because they had been led to
believe that void was an acceptable return from main either because of an
incorrect textbook or a compiler that accepts it?

I wouldn't give him 'thumbs down', but I'd attempt to educate him.

-Mike
 
J

J. Campbell

John Harrison said:
The way to do this is with if ... else

if (numbers are ascending)
printf("ascending");
else if (numbers are descending)
printf("descending");
else
printf("no order");

See how simple that is.
john

What's wrong with an doing it using the oft-maligned-yet-ever-so-clear
?: operator rather than the ever-so-passe if operator?

#include<iostream>

int main(){

std::cout << "Enter 3 numbers: ";
int x , y, z;
std::cin >> x >> y >> z;

std::cout << "The numbers are in " <<
(x < y ? y >= z ? "no":"ascending": y <= z ? "no":"decending")
<< " order" << std::endl;
}
;-)
 
M

Mike Wahler

J. Campbell said:
"John Harrison" <[email protected]> wrote in message

What's wrong with an doing it using the oft-maligned-yet-ever-so-clear
?: operator

The '?:' operator appears 'clear' to some, not so to others.
Also, its clarity often depends upon context.
rather than the ever-so-passe if operator?

'if' is not an operator, it's a keyword. Your labeling of it
as 'passe' just indicates to me that you place 'cleverness'
above clarity. 'if' is one of most clear constructs in the
language, as are e.g. 'while' and 'goto'. Even a nonprogrammer,
upon seeing 'if' in source code, can deduce that it represents
a decision point in the logic.

#include<iostream>

int main(){

std::cout << "Enter 3 numbers: ";
int x , y, z;
std::cin >> x >> y >> z;

std::cout << "The numbers are in " <<
(x < y ? y >= z ? "no":"ascending": y <= z ? "no":"decending")
<< " order" << std::endl;

I submit that the above expressed with 'if/else' is much more quickly
grasped by most folks than with '?:' Especially since you've used
a 'nested' '?:'.

-Mike
 
J

Joe C

The '?:' operator appears 'clear' to some, not so to others.
Also, its clarity often depends upon context.


'if' is not an operator, it's a keyword. Your labeling of it
as 'passe' just indicates to me that you place 'cleverness'
above clarity.
-Mike

I was joking :-/

-Joe
 
R

Ron Natalie

Mike said:
Of course. A productive programmer is expected to write
programs that work correctly. A C++ program which defines
'main()' to return void, has absolutely no guarantee that
it will work correctly, or at all.

Actually, any compiler which fails to issue a diagnostic, for
such is defective. The standard is clear. This isn't undefined
behavior, the program is ill-formed.

Actually the smallest well-formed program is
int main() { }

Main gets a stupid dispensation on return values.
 
M

Mike Wahler

Ron Natalie said:
Actually, any compiler which fails to issue a diagnostic, for
such is defective. The standard is clear. This isn't undefined
behavior, the program is ill-formed.

Yes. But is an ill-formed program guaranteed to work correctly? :)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top