Referencing a variable in another function

Hello Everyone,

Hello Everyone,

EDIT:
I am trying to reference a variable in my class called yolo. The variable is from an input that I want to use in my main (if possible) so I can keep running my called methods in a loop.
I am dead in the water on this one. Thanks

I get the following error below. I know its not in my 'scope' its in my printscreen method. Any suggestions?

 
100	9	C:\Users\carlos\Documents\My Projects\C++\rps.cpp	[Error] 'confirm' was not declared in this scope


I can't reference it basically from my main. If you have any suggestions or fixes please let me know. thanks

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	rps yolo;
	do
	{
	yolo.takeInputs();
	yolo.compareInputs();
	yolo.printScreen();
	return 0;
	}while(confirm== "y" || confirm == "Y");
return 0;
}




Regards,

SunDontShine
Last edited on
your error has nothing to do with your class.
You have not declared the variable 'confirm'. If it's in your printScreen method you could return it from this function so you can use it in main()

also your return statement on line 9 makes no sense. You will reach that line and exit your program.
Last edited on
Yeh I just threw the return 0 bc I thought main always had to return an int if my method doesn't ...

For returning the variable "confirm" which is in the scope of my print screen method... How would the
syntax be in the while loop? I just curious bc I'm on the way to school n going to work on it there.
Yeh I just threw the return 0 bc I thought main always had to return an int if my method doesn't ...

I'm not sure what you mean by that. By adding a return 0 there your do while will loop once and only once.

How would the
syntax be in the while loop?

something like this maybe:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	rps yolo;
        char confirm('n');   // some default initialisation
	do
	{
	    yolo.takeInputs();
	    yolo.compareInputs();
	    confirm = yolo.printScreen();

	}while(confirm== "y" || confirm == "Y");
return 0;
}


However, your method is called printScreeen() so in theory that's all it should be doing.
You could/should maybe ask the user stuff in your main.
if confirm is a char, you would have to compare it to a char literal: 'y' (single quotes).... not a string literal "y" (double quotes)
Hello,

Mutexe, sorry for the short response i closed my window. That works . I just took the variable out of my function and put it in my main.

Another question though.

I wanted to create a while loop that checks the input, if it matches one of the characters in my array

i have this so far but i know its going to keep looping over and over.
my array is
 
	string answers[6]={"R","r","P","p","S", "s"};


Leaner is just a boolean i have set as false
1
2
3
4
5
6
while(leaner)// while loop will verify the letters are acceptable or not and makes users change them.
			{
				cout << "Please enter an Acceptable letter representing (R)ock, (P)aper, or (S)cissors: ";
				cin >> p1hand;
				cout << endl;
			}


Thanks
Last edited on
Topic archived. No new replies allowed.