Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » (hopefully)simple C++ problem
Closed Thread
Old 01-11-2006, 05:45 PM   #1 (permalink)
 
Newb Techie

Join Date: Oct 2005

Posts: 13

flyacrossawave

Default (hopefully)simple C++ problem

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.
flyacrossawave is offline  
Old 01-11-2006, 10:36 PM   #2 (permalink)
 
Newb Techie

Join Date: Aug 2005

Posts: 22

Cache

Default

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.
Cache is offline  
Old 01-11-2006, 11:05 PM   #3 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

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.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 01-12-2006, 04:18 PM   #4 (permalink)
 
Newb Techie

Join Date: Oct 2005

Posts: 13

flyacrossawave

Default

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

flyacrossawave is offline  
Old 01-12-2006, 09:09 PM   #5 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

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)
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
Old 01-15-2006, 07:46 PM   #6 (permalink)
 
Newb Techie

Join Date: Oct 2005

Posts: 13

flyacrossawave

Default

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.
flyacrossawave is offline  
Old 01-16-2006, 09:57 PM   #7 (permalink)
 
Newb Techie

Join Date: Aug 2005

Posts: 22

Cache

Default

Try this:

Code:
getline(cin, response);

Cache is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On