My do-while loop doesn't end

I am new to C ++, so sorry if this question is really simple. I am writing a C ++ program that rolls a cube and shows its number until the user types the word cancel , but my cycle does not end, although I type cancel . Here is my code (I am using C ++ dev):

#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; } 
+4
source share
7 answers
  • you have to fix

     }while(k!="cancel"); 

    in

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

    or it is better to use a standard string class if you are already using C ++

    Here is a link for strcmp , and here you have an example comparing two lines of C ++ .

  • try to roll the dice several times, and then you will realize that you are getting the same random value, because the time () used for the random seed will return the same second at startup in the same second. Therefore, you must move:

     srand(time(NULL)); 

    into your main function before the do while loop, and you don’t have to roll the dice 100 times, you can only get one line of code in the dice:

     return (rand()% 6)+1; 
+2
source

This will never be true because you are comparing pointers, not the contents of a string. Another reason you should use std::string (the comparison operator will compare the string itself for this).

Using this method, you can use strcmp , the C ++ method is to use std :: string and rely on its comparison operators (namely operator== ). But since it is tagged with C ++, I highly recommend that you use std::string .

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

+6
source

Use the std :: strcmp function. You are simply comparing a pointer to a string literal. But, like others, you really should use std :: string.

+3
source

!= for raw character strings does not work the way you think. It does not compare the strings themselves, just the addresses of pointers, which, of course, will always be different.

Use strcmp in this case.

+1
source

you should study the use of cin.getline instead of cin β†’ k, more specifically it will consider spaces in the input file if the user enters them.

EDIT: Also, as many others have mentioned, you should use strings, not literals.

0
source

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

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

You cannot compare char strings like this only by comparing std::string , since you are comparing the "k" pointer with the "cancel" constant pointer. You should use strncmp , but since you are using C ++, you should use std:string .

Also, if the user enters a string longer than 7 characters, this will cause unpredictable behavior, since you will overflow the buffer using std::string to avoid this.

0
source

Source: https://habr.com/ru/post/1491727/


All Articles