C++ assignment question

Pages: 12
That's what he tried the first time, which is not what he needs. We've already resolved this.
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 :]
Do you know what a variable is?

(That isn't meant to sound patronising)
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.
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?
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
would I use cin AND cout?
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 >> 
Ohhhh okay. That makes a little more sense. So how would I write that to make a 'C'

int c = x;
cin >> c ????
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.

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??
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.
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 :/
I know once it clicks I'm going to feel like an idiot. lol
I got it! I was having a problem with this too.
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";

}
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