•
•
•
•
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
![]() |
•
•
Join Date: Jun 2004
Posts: 604
Reputation:
Rep Power: 6
Solved Threads: 6
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
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
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
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
•
•
Join Date: Jul 2005
Posts: 1,095
Reputation:
Rep Power: 9
Solved Threads: 141
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.
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- C command-line I/O question (C++)
- question on cooling (Cases, Fans and Power Supplies)
- Context-sensitive grammar question :( (Computer Science and Software Design)
- Welcome PC Mod Kingdom peeps! (Geeks' Lounge)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
- Changing Network Configuration (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Search a txt file for words
- Next Thread: C/C++/java



Linear Mode