Computing.Net > Forums > Programming > C++ Beginner, PLEASE explain this!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Beginner, PLEASE explain this!

Reply to Message Icon

Name: serpx
Date: June 22, 2005 at 23:24:46 Pacific
OS: Windows XP Home Edition
CPU/Ram: Pentium(R) and 384 MB of
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 23, 2005 at 00:07:46 Pacific
Reply:

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.


0

Response Number 2
Name: Fozzie
Date: June 23, 2005 at 13:09:59 Pacific
Reply:

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!


0

Response Number 3
Name: serpx
Date: June 23, 2005 at 13:45:29 Pacific
Reply:

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


0

Response Number 4
Name: Fozzie
Date: June 23, 2005 at 22:04:32 Pacific
Reply:

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.


0

Response Number 5
Name: serpx
Date: June 24, 2005 at 05:03:40 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Sord
Date: June 24, 2005 at 14:00:02 Pacific
Reply:

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


0

Response Number 7
Name: dgcoder
Date: July 4, 2005 at 14:24:46 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

help with batch file plea... download/update script



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Beginner, PLEASE explain this!

c++ beginner www.computing.net/answers/programming/c-beginner/3521.html

Copy Command in a Batch File www.computing.net/answers/programming/copy-command-in-a-batch-file/6556.html

Formatted Printing??? www.computing.net/answers/programming/formatted-printing/1899.html