How i get this to return to the question?

i need this code to return to the question if you didnt type yes/no. if you type inventory it just says your inventory and pauses and closes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(){
	srand(time(0));    /// Random number generator based on the cpu's time.
	r = 1+rand()% 1;  /// It can only go up to ten, At the moment.
	if(r == 1){
		cout<<"if you type in inventory when you can type it tells\nyou your inventory"<<endl;
		cout<<"\n\n"<<endl;
		cout << "You awake in a humid forest.\nYou see two wolves fighting over a piece of meat,\nthey look angry.\nYou know that wolves may attack if you make sudden movements.\nYou slowly arise to see both of the wolves are staring at you,\ntheir eyes burning holes through your skull.\n" << endl;
		system("pause");
		cout <<"Beside you is a stick that looks to have a sharp tip, use the stick, yes/no?"<<endl;
		items = items + stick;
/// function for inventory
 cin>>y;
 if(inventory==y){
cout<<items<<endl;
}
system("pause");
	}
Last edited on
You can do it two ways.

1. Make a while loop and if the condition, lets say 'y' is like 5 then break out of it and let it stop the program. Here is an example:
1
2
3
4
5
6
while(y != 5){
    cin >> y;
    if(inventory == y){
        cout << items << endl;
    }
}


2. make a goto statement, here is an example:
1
2
3
4
5
6
ask:
  cin >> y;
  if(inventory==y){
        cout << items << endl;
  }
  goto ask;
Last edited on
closed account (Dy7SLyTq)
DO NOT USE GOTO!!!! it can make for confusing and poor code. the second way can be a do while loop instead (or recursive functions, but thats overkill for something this small)
so i get whatcha mean and all but another Question is, if i do

while(y != yes , no){ ect



if i do that and want to use the stick how do i do it so if i type yes(because it isnt no itll keep the loop going)?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int r;
string inventory="inventory";
string y;
string stick="1 sharp stick";
string items;
string Y="yes";
string N="no";
char response;

/// function for inventory
/// cin>>y
/// if(inventory==y){
///cout<<items<<endl;
///}
///system("pause");

int main(){
srand(time(0)); /// Random number generator based on the cpu's time.
r = 1+rand()% 1; /// It can only go up to ten, At the moment.
if(r == 1){
cout<<"if you type in inventory when you can type it tells\nyou your inventory"<<endl;
cout<<"\n\n"<<endl;
cout << "You awake in a humid forest.\nYou see two wolves fighting over a piece of meat,\nthey look angry.\nYou know that wolves may attack if you make sudden movements.\nYou slowly arise to see both of the wolves are staring at you,\ntheir eyes burning holes through your skull.\n" << endl;
system("pause");
cout <<"Beside you is a stick that looks to have a sharp tip, use the stick, yes/no?\nPlease type 1 for yes and 2 for no."<<endl;
items = items + stick;
/// function for inventory
while(response == Y ){
cin>>y,response;
if(inventory==y){
cout<<items<<endl;
}
}
if(YN == 1){
cout<<"you grab the stick and stab one of the wolves as they bound for you."<<endl;
}
}
system("pause");

heres my code with it edited a lil but its still broke.
Last edited on
@CrazyKane
if i do

while(y != yes , no){ ect


this statement is the same as:
while(y != no){ ect

i couldn't find a link on the comma operator, i'll have to demonstrate it myself:
the comma operator is a binary operator, it takes two expressions, evaluates them both, and the big expression evaluates to the same as the right operand.
example:
(x=0) , (y=3); is the same as x = 0; y = 3;

z = (x = 0) , (y = 3); will evaluate to the same result as: x = 0; z = (y = 3);

one step further:
z = ( std::cin>>x) , (y = x+3) ;, will give the same result as: std::cin>>x; z = (y = x+3);

since the comma operator evaluates to its right operand, when you combine two or more comma operators, they will evaluate to the rightmost operand:
z = (std::cin>>x) , (y = x+3) , ( n = std::pow(y,5) ) , (std::cout<<n) , (n);
will evaluate to the same as:
1
2
3
4
5
std::cin>>x;
y = x+3;
n = std::pow(y,5);
std::cout<<n;
z = n;


finally, using the comma operator heavily can cost loss in readability, while using it rarely in a smart way can give considerable benefits, like in loops:
1
2
3
for(int i=0,j=SOME_VALUE ; i<j ; i++,j--)
{
   ...
I think i get what you mean, sorry i'm a noob
Topic archived. No new replies allowed.