简体   繁体   中英

my do-while loop doesn't end

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;        
}

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 .

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.

  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; 

!= 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.

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.

您应该使用strcmp函数来比较C样式字符串:

 }while(strcmp(k, "cancel") != 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM