Basic C++ question...

Discuss all aspects of programming here.

Moderator: The Mod Squad

Basic C++ question...

Postby Snowfire » Wed Sep 15, 2004 5:23 pm

Hey guys, I just started C++, and I was wondering what would be a basic way to find the lowest and highest number when the user inputs 3 different integers. I know that in Visual Basic when you used an if statement you could do something like:

If ( int1 < int2 ) and ( int1 < int3 ) then

But in C++ I know that I can't use THEN, and I can't compare more than 2 numbers. Any idea how to do that with just a basic <iostream> ?
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby Hardware Junkie » Wed Sep 15, 2004 6:36 pm

VISUAL BASIC:


If ( int1 < int2 ) and ( int1 < int3 ) then
COUNTER = 1
END




Would be this in C/C++:



If ( int1 < int2 || int1 < int3 )
COUNTER == 1;
"Imagination is the only weapon in the war against reality." -Jules de Gautier

Image
Hardware Junkie
Mobo-fu Master
Mobo-fu Master
 
Posts: 19405
Joined: Thu Jan 25, 2001 1:01 am
Location: 00000h - 0000Fh

Postby Snowfire » Thu Sep 16, 2004 12:33 pm

Ok, that works great. I have one last problem with my program though. Here's my script:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
int int1, int2, int3, sum, ave, prod;

cout << "Please enter 3 integers: ";

cin >> int1 >> int2 >> int3;

sum = int1 + int2 + int3;

cout << "The sum is " << sum << endl;

ave = (int1 + int2 + int3) / 3;

cout << "The average is " << ave << endl;

prod = int1 * int2 * int3;

cout << "The product is "<< prod << endl;

if (int1 < int2 || int1 < int3)
cout << "The lowest is " << int1 << endl;

if (int2 < int1 || int2 < int3)
cout << "The lowest is " << int2 << endl;

if (int3 < int1 || int3 < int2)
cout << "The lowest is " << int3 << endl;

if (int1 > int2 || int1 > int3)
cout << "The highest is " << int1 << endl;

if (int2 > int1 || int2 > int3)
cout << "The highest is " << int2 << endl;

if (int3 > int1 || int3 > int2)
cout << "The highest is " << int3 << endl;

return 0;
}

Itdoes everything it's supposed to, but when I put in (for example) the numbers 3 5 and 9, it does the lowest and highest 2 times, when I just want it once, idea?
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby dr_st » Thu Sep 16, 2004 1:09 pm

That's OR: ||

That's AND: &&
dr_st
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2191
Joined: Tue May 11, 2004 11:18 pm
Location: Outworld

Postby Snowfire » Thu Sep 16, 2004 1:47 pm

Ahh, I thought something was wrong with that! :roll: Thanks for the help guys! :D
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby Hardware Junkie » Thu Sep 16, 2004 5:04 pm

Its been awhile since I drabbled in programming. Thanks for the clarification.
"Imagination is the only weapon in the war against reality." -Jules de Gautier

Image
Hardware Junkie
Mobo-fu Master
Mobo-fu Master
 
Posts: 19405
Joined: Thu Jan 25, 2001 1:01 am
Location: 00000h - 0000Fh

Postby Snowfire » Thu Sep 16, 2004 6:06 pm

Crap, now I have another question. :? I want to come up with a script where the user inputs a 5-digit number, seperates the number into it's individual digits and prints the digits seperated from one another by 3 spaces each. Apparently the hint my book gives is, "Use the integer division and modulous operators." and it gives an example of what it should look like, if the user inputed 42339, then the program should print:

4 2 3 3 9

I think that the "integer division and modulous operators" have me confused. What do they have to do with it? Also, just overall I think, WHAT THE HECK? 8O I think that being gone and majorly sick, didn't help me at all... :cry:
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby dr_st » Fri Sep 17, 2004 12:30 am

In C integer division is the / operator. Integer remainder (modulus) is the % operator.

42339 / 10 = 4233
42339 % 10 = 9

4233 / 10 = 423
4233 % 10 = 3

etc.
dr_st
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2191
Joined: Tue May 11, 2004 11:18 pm
Location: Outworld


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests