User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,071 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,248 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1024 | Replies: 3
Reply
Join Date: Jun 2004
Posts: 604
Reputation: freesoft_2000 is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 6
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Help C++ Refresher Question

  #1  
Aug 16th, 2006
Hi everyone,

I have not done C++ for quite sometime now and seems that i may have forgotten a bit so please bear with me a while.

I am currently trying to output some text to the console in c++, but seems i may have forgotten something because the text does not output correctly. It is incorrect because each time there is a space in the string only the first string is printed to the console.

This is the code

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <strings.h>
#include <stdio.h>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{

string a;
string b = "This is a test string";
int c = 0, d = 0;

cout<< "Input a string\n";

cin>> a;
cout<<endl;

d = a.compare(b);

if(d == 0)
{
cout<< "Both the strings are the same\n";
cout<< "The below is string a\n";
//The below is where the problem occurs if the string is
//the same and only the word "This" is printed
//to the console

cout<< a
cout<<  "\n";
cout<< "The below is string b\n";
cout<<  b;
cout<<  "\n";
}

else
{
cout<< "Both the the strings are different\n";
cout<< "The below is string a\n";
cout<<  a;
cout<<  "\n";
cout<< "The below is string b\n";
cout<<  b;
cout<<  "\n";
}

cout<<endl; 
system("PAUSE");
return 0;
}

I am currently using DevC++.

Looks like i have become a bit rusty, please bear with me on this question.

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond

Tell me what type of software do you like and what would you pay for it

http://www.daniweb.com/techtalkforums/thread19660.html
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Rep Power: 4
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: C++ Refresher Question

  #2  
Aug 16th, 2006
Change cin>>a ; to getline(cin,a);
and remove #include <strings.h> and #include <stdio.h>
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote  
Join Date: Aug 2006
Posts: 3
Reputation: daprasIT is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daprasIT daprasIT is offline Offline
Newbie Poster

Re: C++ Refresher Question

  #3  
Aug 16th, 2006
Why getline, and not cin for such a simple application?

Is getline a better option for string manipulation or something?
Reply With Quote  
Join Date: Jul 2005
Posts: 1,095
Reputation: Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough 
Rep Power: 9
Solved Threads: 141
Lerner Lerner is offline Offline
Veteran Poster

Re: C++ Refresher Question

  #4  
Aug 16th, 2006
The >> will stop input into the desired variable at the first white space char it encounters. Whitespace characters include the space, tab, and newline char in addition to EOF indicator.

getline(), whether the version for C style strings or the version for STL strings won't stop at any whitespace char. It will stop at the designated char, which is the newline char by default, but can be any char you wish.

getline() is a member of the iostream class as is the >> operator. Therefore it's not getline() vs cin but getline() vs >>. Both getline() and the >> operator are overloaded in the STL string class so they can be used with STL strings in a very similar fashion to what they are used otherwise.

In the example posted the only files needed are the iostream and string headers. IF you want to include some of the older C style files AND your compiler is namespace compliant, then you would be better off using the updated C header files, which all start with the letter c and don't have the .h extension. So it would be cstdlib instead of stdlib.h and cctype instead of ctype.h and cstdio instead of stdio.h, etc.
Last edited by Lerner : Aug 16th, 2006 at 5:39 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC