7 Replies - 926 Views - Last Post: 09 February 2017 - 06:40 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 815
  • Joined: 15-December 14

Learning C++ and I have a question about this loop

Posted 01 August 2016 - 08:00 PM

Learning C++ after finishing python. I was curious if it was possible to do this in C++. In python I could make a program like this:

while True:
ans=raw_input("Do you want to exit the game? <yes/no>\n")
if ans =="yes":
break
else:
pass

It would make the user either to continue the loop or exit the loop. I was learning about loops in C++ and I tried to make a similar program. Just curious if it is possible and how close or far I was in my program. I was looking around online to find the answer and a few things but nothing close to what I wanted to do. I wasn't sure if I could do this in a while loop or if I had to use a do while loop.

This is what I tried to do in my C++ program

#include<iostream>
using namestream std;

int main()
{
    int a=0;
    while(a!=1)
    {a++;
    ans=cin>>"Do you want to leave the game? <yes/no>>\n";
    if (ans==yes){
        break;
    }
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Learning C++ and I have a question about this loop

#2 jimblumberg   User is online

  • member icon

Reputation: 5788
  • View blog
  • Posts: 17,697
  • Joined: 25-December 09

Re: Learning C++ and I have a question about this loop

Posted 01 August 2016 - 09:25 PM

Quote

This is what I tried to do in my C++ program

Does the code compile without errors or warnings?

If so does it produce the desired ouput?

If not what seems to be wrong?

Jim

This post has been edited by jimblumberg: 01 August 2016 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 815
  • Joined: 15-December 14

Re: Learning C++ and I have a question about this loop

Posted 03 February 2017 - 03:26 PM

I got ahead of myself and thought c++ would be as simple as python to make a while loop to handle the program. I've made progress in my lessons, sorry it took so long to reply. I've tried using a switch statement to try and have the same result I had using python. I've tried making a switch statement with the word yes without quotes in it and then with quotes in it. I'm just wondering if I'm on the right track and whether or not it is possible to make that kind of program using c++. This is my program so far.

#include<iostream>
using namespace std;

int main()
{
	int "yes";

	switch("yes");
	{
	cout<<"Do you want to exit the game?.<yes/no>"<<endl;
	case "yes":
	break;
	case "no":
	default:
		cout<<"Choose one of the options."<<endl;
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Learning C++ and I have a question about this loop

Posted 03 February 2017 - 03:34 PM

C++ is not Python,

it doesn't work on the same principles.

C is just one level above machine code and C++ is just one level above C.

You need a book on C++,

a beginners book...
Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Learning C++ and I have a question about this loop

Posted 03 February 2017 - 03:35 PM

You cannot perform a switch on string types in C++. You can you an if-else and use the comparison operator.

#include<iostream>
#include <string>
using namespace std;

int main()
{
	string str = "yes";
     
        if(str == "yes") {
           //do something
        }
        else if(str == "no") {
           //do something
        }
        else {
            // str is neither of the above
        }

	return 0;
}




Here is a simple translation from the python your provided in your original post, it may help seeing the difference.

python
while True:
ans=raw_input("Do you want to exit the game? <yes/no>\n")
if ans =="yes":
break
else:
pass



c++
#include <iostream>
#include <string>

using namespace std;

int main() {
   while(true) {
      cout << "Do you want to exit the game? <yes/no>\n";

      string input;
      cin >> input;

      if(input == "yes") 
         break;
   }


   return 0;
}


This post has been edited by jjl: 03 February 2017 - 03:38 PM

Was This Post Helpful? 1
  • +
  • -

#6 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 815
  • Joined: 15-December 14

Re: Learning C++ and I have a question about this loop

Posted 03 February 2017 - 11:14 PM

I actually have been using a book as well as any resource I could find to answer my questions and help me along the way to learning c++. I was learning about the different types of loops and wondered if it was possible to do the very same thing in c++. My following lesson was about switch and I tried to use the switch function to do make it work (which it didn't).
Was This Post Helpful? 0
  • +
  • -

#7 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Learning C++ and I have a question about this loop

Posted 07 February 2017 - 12:50 PM

Just as a side note to this thread, C++11 introduced constexpr which allows for compile time evaulation, which allows the compiler to replace the expression with a constant expression. This can be used to hash string literals at compile to so they can be used in a switch statement with zero runtime overhead.

i.e.
#include<iostream>
#include <string>
using namespace std;

constexpr 
unsigned int hash(const char* str, int h = 0) {
    return !str[h] ? 5381 : (hash(str, h+1)*33) ^ str[h];
}


int main() {
   string str = "yes";
   
   switch(hash(str.c_str()) {
      case hash("yes"):   
      cout<<"Do you want to exit the game?.<yes/no>"<<endl; 
      break;

      case hash("no"):
      break;

      default:
      cout<<"Choose one of the options."<<endl;
   }
     

   return 0;
}


This post has been edited by jjl: 07 February 2017 - 12:51 PM

Was This Post Helpful? 1
  • +
  • -

#8 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 815
  • Joined: 15-December 14

Re: Learning C++ and I have a question about this loop

Posted 09 February 2017 - 06:40 PM

jji that actually answers my question. I like to complete the lessons I learned and then apply it to my own programs as well as what I learned previously. I understand why and how you use a switch statement, but I honestly couldn't find a use for it aside from what the teacher showed in the lesson and another tutorial on youtube about asking how many legs you had and the user would key in the number of legs. Thank you all for your help. I really appreciate any and all help as well as suggestions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1