Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iBookG4user

macrumors 604
Original poster
Jun 27, 2006
6,595
2
Seattle, WA
Hello,
I've toyed with the idea of getting into programming in the past and recently dove in and took it as one of my classes this semester. I'm still trying to wrap my head around it, but I would like some guidance for this assignment. We're supposed to write a program that takes three input numbers (eg a, b, c) and then output them both forwards and backwards (c, b, a).
Here's the code that I've written so far:

#include <iostream>
using namespace std;
int main()
{
int a, b, c;

cout << "Enter 3 numbers " << endl;
cin >> a, b, c

cout << "Your numbers forwards" << a, b, c << endl;
cout << "Your numbers backwards" << c, b, a << endl;

return 0;
}

EDIT:
I actually figured it out on my own (I seem to be doing that a lot lately after I create a thread...), anyways, here's the code that I ended up with:

#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;

cout << "Enter a value " << endl;
cin >> a;

cout << "Enter a value " << endl;
cin >> b;

cout << "Enter a value " << endl;
cin >> c;

cout << "Your numbers forwards" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;

cout << "Your numbers backwards" << endl;
cout << c << endl;
cout << b << endl;
cout << a << endl;
return 0;
}
 

Cinder6

macrumors 6502a
Jul 9, 2009
509
50
You can do the input this way:

Code:
cin >> a >> b >> c;

Output:
Code:
cout << "Your numbers forward " << a << b << c <<endl;

You'd have to put spaces between the outputs, though, or they would all cluster together.
 

iBookG4user

macrumors 604
Original poster
Jun 27, 2006
6,595
2
Seattle, WA
You can do the input this way:

Code:
cin >> a >> b >> c;

Output:
Code:
cout << "Your numbers forward " << a << b << c <<endl;

You'd have to put spaces between the outputs, though, or they would all cluster together.

Ok, that makes more sense. That's where I was going wrong the first time, the professor instructed us to have each number on a new line in the output. So with that code, you would put "/n" after each value, correct?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Ok, that makes more sense. That's where I was going wrong the first time, the professor instructed us to have each number on a new line in the output. So with that code, you would put "/n" after each value, correct?

best to use std::endl between each stream insertion operator (<<). \n is OK, but less portable.

-Lee
 

uaecasher

macrumors 65816
Jan 29, 2009
1,289
0
Stillwater, OK
You can also use:
Code:
 cin >> a; cin >>  b; cin >> c;

 cout << "Your numbers forwards" << endl << a << endl <<  b << endl << c << endl;
 cout << "Your numbers backwards" << endl << c << endl <<  b << endl <<  a << endl;
 

jbrenn

macrumors 6502a
Aug 27, 2008
638
0
The second is better and will work. The main problem with the first is if they enter 3.25 4 5 the value of A will be 3 B will be 25 C=4. I dont think the teacher would try to trick you on the first problem. I would set the ints =0 when i create them. doing that will help later on trying to troubleshoot logic errors.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.