C++ assignment question

Pages: 12
Feb 20, 2013 at 1:44am
That's what he tried the first time, which is not what he needs. We've already resolved this.
Feb 20, 2013 at 1:52am
I have no idea what I need. lol it's literally a foreign language to me right now haha. I'll figure it out though :]
Feb 20, 2013 at 2:13am
Do you know what a variable is?

(That isn't meant to sound patronising)
Feb 20, 2013 at 2:23am
lol no worries. all I know about variables is that it's a memory location. I'm on chapter 1 of this book and my teacher hasn't taught us anything yet. Last week was just orientation and somehow I'm supposed to write these programs. I wrote 3 already, this is the only one I'm having trouble with.
Feb 20, 2013 at 2:26am
Well this is just an exercise of storing user input into a variable and then using that variable. Do you know how to output the value of a variable?
Feb 20, 2013 at 2:32am
probably, I don't know all the tech terms yet lol. I know what I'm doing, somewhat, but I don't know how to explain anything yet lol
Feb 20, 2013 at 2:39am
would I use cin AND cout?
Feb 20, 2013 at 2:44am
Yes you would. You use cin to get input, and cout to output. For example:

1
2
3
4
5
6
7
int n = 9; //n initialized to 9
std::cin >> n; //Now the user will be prompted to provide input
//Let's say you type in 37 for the input
std::cout << n; //Now the value stored in n will be outputted to your terminal.

//Note the different operators here. << is the insertion operation, while >> is the extraction operator. In C days these meant something entirely different, but C++ offers something called operator overloaded. Don't worry about any of that right now, just keep in
//mind that to output you will use << and to get input you will use >> 
Feb 20, 2013 at 2:50am
Ohhhh okay. That makes a little more sense. So how would I write that to make a 'C'

int c = x;
cin >> c ????
Feb 20, 2013 at 3:30am
You got it. Except int c = x; doesn't make sense unless x is already declared and initialized somewhere as an int. In this situation you could just write int c; and then cin >> c; like you have.

I wrote this on my phone, so forgive any typos/seeming lack of effort.
Feb 20, 2013 at 3:38am

int main()
{

int c;
cin >> c;
cout << " X X X\n";
cout << " X X\n";
cout << "X\n";
cout << "X\n";
cout << "X\n";
cout << "X\n";
cout << "X\n";
cout << " X X\n";
cout << " X X X\n";

return 0;
}


does that look right??
Feb 20, 2013 at 3:55am
Ah not quite. You're still outputting a hardcoded x. Look at my example above on how to output a variable value.

std::cout << c << c << std::endl;

This will output the value of c twice, and then a newline. See if you can use this to fix your assignment.
Feb 20, 2013 at 4:15am
I'm still not understanding something. How do I change the value so it comes up as an 'x' without typing a hardcoded x. and now it won't run on the output :/
Feb 20, 2013 at 4:15am
I know once it clicks I'm going to feel like an idiot. lol
Feb 20, 2013 at 6:13am
I got it! I was having a problem with this too.
Feb 20, 2013 at 9:02am
hope this makes it clearer:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

int main()
{
	//declare variables
	char input;  //char since you want characters and not numbers

	std::cin >> input;  //read users input

	//print a big C consisting of single "X"
	std::cout << " X X X\n";
	std::cout << " X X\n";
	std::cout << "X\n";
	std::cout << "X\n";
	std::cout << "X\n";
	std::cout << "X\n";
	std::cout << "X\n";
	std::cout << " X X\n";
	std::cout << " X X X\n";
	std::cout << "\n\n\n";
	
	//print a big C consisting of the users input
	std::cout << input << input << input << "\n";
	std::cout << input << input << "\n";
	std::cout << input << "\n";
	std::cout << input << "\n";
	std::cout << input << "\n";
	std::cout << input << "\n";
	std::cout << input << "\n";
	std::cout << input << input << "\n";
	std::cout << input << input << input << "\n";

}
Feb 20, 2013 at 7:04pm
I got it!! all makes sense now. Thank so much for all your help guys, I really appreciate it, and I will probably be back with more questions lol
Topic archived. No new replies allowed.
Pages: 12