(hopefully)simple C++ problem

Status
Not open for further replies.

flyacrossawave

Solid State Member
Messages
13
I am having trouble with char stings. I attempt to create loops where the loop ends when the char variable becomes a certain string. For example:

Code:
#include <iostream.h>

int main()
{
 char toquit[100];
 cout << "Enter 'quit' to exit" << endl;
 while (toquit != "quit")
 {
      cin >> toquit;
      cout << "The program should still be running during this loop."
      << endl;
 }
 char response;
 cin >> response;
 return 0;
}

This loop does not end after my entering "quit", it simply produces the text of the cout statement again no matter what input is given. My compiler is Dev-C++ Version 4.

Any help in this matter would be greatly appreciated.
 
For situations like this, don't even bother messin' around with C style strings. Your programming in C++, so make use of the std string.
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string toquit = "";
    cout << "Enter 'quit' to exit" << endl;
    cin >> toquit;
    
    while (toquit != "quit")
    {
      cout << "The program should still be running during this loop." << endl;
      cin.sync();
      cin >> toquit;
    }
    
    return 0;
}

Also, get rid of the .h extension from your header files. That's what all the whining Dev has no doubt been doing about depreciated header file.
 
You can not test equality with C-style null delimited strings. You have to realize that in C, strings are just a pointer to a null delimited char array.

So you need to call some function (strcmp() i think) to compare the two strings.

What you are actually doing there is "pointer arithmetic" so, what the compiler would actually do is compare the values contained in toquit and whatever memory address the compiler assigned to the "quit" string literal.
 
Thank you Cache, that was exactly what I needed, and thanks to TheHeadFL as well. The only problem is, I am having trouble using cin.get and cin.getline with variables of the type string. I change the variable type to char and it works perfectly, but I need it to be string type for the program to work overall.

For example:

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
 {
 string response;
cout << "Enter 'quit' to quit, otherwise, just type stuff." << endl;

 while (response != "quit")
        {
        cin.getline(response, 1000)
        cin.ignore(100, '\n');
        }
 return 0;
 }
 
Can you post the exact errors? I am guessing (its been a LONG time) that getline returns a null delimited C-style string using pass-by-reference.

In that case, you'd want:

Code:
char *buffer;
int nChars;
nChars = cin.getline(buffer,1000);
response = buffer;

You may have to perform some method call to set a string object using a char array, but that is pretty close to what you need.

(The nChars bit is not really needed but is good practice since the length of the returned char array is not known at compile time)
 
this is the error I am getting:

no matching function for call to `_IO_istream_withassign::getline (string &, int)'

Thanks for the code TheHeadFL, but I can't seem to get my program to work with it. The error that comes up is:

`class istream' used where a `int' was expected

If anyone else has any ideas, all I need to do is find a way to get some equivalent of cin.getline input using a string variable, and would much appreciate any help.
 
Status
Not open for further replies.
Back
Top Bottom