4

i'm new to c++ so sorry if this question is really simple.i'm writing a program in c++ that rolls a dice and shows it's number until the user types the word cancel but my loop doesn't end even though i type cancel.here is my code(i use dev c++):

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int dice (int);
int main()
{
    char k[7];
    int x;
    do
    {
          cout<<"your dice number is: "<<dice(x)<<endl;
          cout<<"do you want to cancel or continue?";
          cin>>k;
     }while(k!="cancel");
          cout<<"END";
          getch();
}
int dice (int a)
{   
    srand(time(NULL));
    for(int i=1;i<100;i++)
        {
            a=(rand()% 6)+1;
        }
            return a;        
}
2
  • thank you for all of your answers.now my program works.thank you. Commented Jul 16, 2013 at 21:28
  • You can accept an answer if it was useful to you (once the timer runs out). Commented Jul 16, 2013 at 21:29

7 Answers 7

6

It will never be true because you are comparing pointers not the actual string content. Yet another reason you should use std::string (the comparison operator for this will compare the string itself).

The C way of doing this comparison is to use strcmp, the C++ way is to use std::string and rely on it's comparison operators (namely operator==). But since this is tagged C++ I strongly suggest you use std::string.

You can find the documentation for strcmp here and the one for std::string here.

Sign up to request clarification or add additional context in comments.

Comments

3

Use the std::strcmp function. You are just comparing a pointer to a string literal. But like others have said, you really should be using std::string.

2 Comments

Haha, I knew this wouldn't stay the answer. All of the other ones were way more thorough.
Indeed. Good self reflection. Have a +1 for the learning experience :)
2
  1. you should correct

    }while(k!="cancel");
    

    into

    }while(strcmp(k,"cancel")!=0);
    

    or better you could use standard string class if you're already using C++

    Here is a reference for strcmp and here you have an example of comparing two C++ strings.

  2. try to throw a dice multiple times and then you realize that you get the same random value, because time() used to seed randomness will return the same second when run in the same second. Therefore you should move:

    srand(time(NULL));
    

    into your main function before the do while loop, and you dont need to throw a dice 100 times, you could get over by just a one line of code in dice:

    return (rand()% 6)+1;
    

Comments

1

!= for raw character strings doesn't do what you think it does. It doesn't compare the strings themselves, just the pointer addresses, which of course will always be different.

Use strcmp in this case.

Comments

0

you should look into using cin.getline instead of just cin>>k, more specifically this will account for spaces in the input should the user enter them.

EDIT: also as many others have mentioned, you should be using strings as opposed to literals.

Comments

0

You should use the strcmp function to compare C style strings:

 }while(strcmp(k, "cancel") != 0);

Comments

0

You can not compare char strings like that, only a std::string can be compared that way as you are comparing the pointer of 'k' to the pointer of the constant "cancel". You should be using strncmp, but since you are using C++ you should be using a std:string instead.

Also you if a user enteres a string longer then 7 characters, it will cause unpredictable behaviour as you will overflow the buffer, using std::string will avoid this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.