6 Replies - 1225 Views - Last Post: 14 July 2010 - 10:16 AM Rate Topic: -----

#1 Cloud Heartnet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-October 09

looping console input

Posted 14 July 2010 - 08:57 AM

I'm working on the following program:
accept lines of text from the console and write the lines of text to the file. An empty string can be used to cause the program to quit accepting lines of text from the console and close the file. To test for an empty string (which occurs when you press the Enter key without typing any text),use the strlen() function from the string.h include file. Is this how it's supposed to look?

 
#include <iostream>
#include <string>
using namespace std;

int main () 
{
    string str;

    
    cout << "Enter string for testing "
    	 << "(0 to quit) : ";
    while ( cin >> str )
    {
    	cout << endl;
    	cout << "String is : " << str << endl;
    	cout << "Enter string for testing "
             << "(0 to quit) : ";
    }
if ( str = 0)
{
cout <<"The End" << endl;
break;
}

    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: looping console input

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: looping console input

Posted 14 July 2010 - 09:32 AM

if ( str = 0)

That sets str to 0.
Was This Post Helpful? 0
  • +
  • -

#3 DJPlayer  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 73
  • Joined: 19-September 07

Re: looping console input

Posted 14 July 2010 - 09:35 AM

string::empty
public member function
bool empty ( ) const;

Test if string is empty
Returns whether the string is empty, i.e. whether its size is 0.

This function does not modify the content of the string in any way. To clear the content of the string, member clear can be used.

example:

string str;

if (str.empty())
    cout << "string is empty" << endl;

Was This Post Helpful? 1
  • +
  • -

#4 Scotty G  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 59
  • Joined: 09-April 10

Re: looping console input

Posted 14 July 2010 - 09:35 AM

you want to use strcmp should look something like

if(strcmp(str, "0") == 0)


hope this helps!

This post has been edited by Scotty G: 14 July 2010 - 09:37 AM

Was This Post Helpful? 1
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: looping console input

Posted 14 July 2010 - 09:35 AM

Also, that if statement is not in the scope of your while loop. Right now, you are stuck in an infinite loop, as that if is designed to execute after the while loop.
EDIT:
if ( str = 0)


str is a std::string. 0, as it is, with no quotes, is an integer. Also, use the equality operator. Jack already pointed the issue.

@scotty:
if(strcmp(str, "0") == 0)


str is a std::string. strcmp expects a pointer to char. Your code will not compile. And why bother using strcmp() with strings, when std::string class already has the operator == overloaded?

This post has been edited by sarmanu: 14 July 2010 - 09:39 AM

Was This Post Helpful? 2
  • +
  • -

#6 DJPlayer  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 73
  • Joined: 19-September 07

Re: looping console input

Posted 14 July 2010 - 09:38 AM

do you want a 0 to quit.. or an empty string?? your question and your program don't coincide.

you have both logic errors and syntax erros.. you might want to rethink just the mere structure of the algorithm before testing for your exit condition.

This post has been edited by DJPlayer: 14 July 2010 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

#7 Guest_quest*


Reputation:

Re: looping console input

Posted 14 July 2010 - 10:16 AM

If your prof says to use strlen ... then you need to use C strings ... (not C++ STL strings) ...

Try this:

#include <iostream>
#include <cstring> // if you really need to use C strings and NOT C++ STL string
using namespace std;

int main()
{
    char str[1024], // make sure big enough to hold your line + '\0' at end
         msg[] = "Enter CString for testing (empty line to quit) : ";
    
    while( cout << msg && cin.getline( str, 1024 ) && strlen(str) != 0 )
        cout << "CString is : " << str << endl;

    cout << "The End\n"
    
         << "\nPress 'Enter' to continue ..." << flush;

    cin.get();
    return 0;
}

Was This Post Helpful? 0

Page 1 of 1