Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey guys! I am pretty new to C++, and I would be very pleased if someone could explain why this occurance occurs. You see, in the code down below, when I type out a string, with spaces, for the first variable, it'll just skip the second input field and end! Why wont this take spaces? (Detailed answer prefered, but a simple answer would be a relief).
How can I make it take spaces?
Saying "Hi, this is Bob!"
And having the program be able to search for "Hi" and say that "Hi" is in the string would be nice. :)Here is the code:
/* '12 Main.cpp' */
/* Input output stream header file */
#include <iostream>/* Start */
main (void)
{
/* Declare two strings */
char FirstString [255];
char SecondString [255];/* Get two strings from the user */
std::cout << "First string: ";
std::cin >> FirstString;
std::cout << "Second string: ";
std::cin >> SecondString;/* Check for second string occurrence */
if ( false == strstr (FirstString, SecondString) )
{
std::cout << "Second string isn't part of the string!" << std::endl;
}
else
{
std::cout << "Second string is part of the string!" << std::endl;
}return 0;
}
-- Jay

Hi,
I'm no programmer, but from the code I've looked at it seems you need:
#include <iostream.h>
and *NOT* :
#include <iostream>
M2
If at first you don't succeed, you're about average.

Well, that's the standard behavior for cin. I believe what you need to use is scanf.
Yes! The Fozman knocks out another "beginner" question!

Almost.
/* 'strstr test.cpp' */
/* Input Output Stream Header */
#include <iostream>
/* Include STDIO header for C keyword. */
#include <stdio.h>main (void)
{
char FirstString [255];
char SecondString [255];printf ("Enter the first string here:");
scanf ("%s",FirstString);
printf ("\nEnterthe second string:");
scanf("%s",SecondString);if (false == strstr(FirstString, SecondString) )
{
std::cout << "This string isn't in the First String!\n";
}
else
{
std::cout << "The string is in!\n";
}return 0;
}But it is acting exactly like std::cin.
Your page says it ignores the whitespace characters before characters ... but it doesn't seem to be doing that.
Compile and run that program with "Hi, I'm Bob!"
And you'll see what I mean, unless it'll actually work fine for you.
-- Jay

I think you can accomplish this with scanf using a loop and the %c type instead of %s and exiting the loop when a newline character is read.
...but, an easier way that I didn't think of earlier is to use cin.getline.
I've never actually used either of these functions in any of my C++ programs but I've seen this question come up before. It's a common topic on this forum and searching the forum can also provide more information for you on related topics.
And if you're interested in manipulating strings, google string.h. It will do some of the work for you. Good luck.

That worked! Thank you very much!
I appreciate you taking your time to help out with this situation. It certinally made a difference. Wonderful people you are. :)
-- Jay

The proper way would be to use getline or fgets and yes it is
#include <iostream> not #include <iostream.h>

IT does not matter <iostream> or <iostream.h> if the compiler has ANSI support.
Try with <string.h> using`
string string_name;
and getting it from keyb via istream`cin>>

![]() |
help with batch file plea...
|
download/update script
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |