Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use spaces?

Status
Not open for further replies.

rjwilldohisbest

Technical User
Jun 2, 2001
79
US
Hi All

I have written a questionare program, begginer here, and when I enter the name part and include a space I get an error in dos and the rest of the screen prints out while leaving the first characters showing and not the ones after the space. Is there a way I can use spaces instead of having to use two cout and cin lines for first and last name? Be nice to combine them on one line with only one cout and cin line

Thanks for your time.

RJ
 
#include <iostream>
#include <string>

using namespace std;

int main()
{
string sFirstName;
string sLastName;

cout << &quot;Enter full name: &quot;;

getline( cin,sFirstName );

cout << sFirstName
<< endl;

return 0;

}

NOTE: If you are coding with VC++ 6.0, there is a bug in the #include <string> file. This bug permits the function getline() to read an extra character after it encounters the delimeter ( in this case '\n' ). This will force the user to hit ENTER twice before the next statment can be performed in the code. To remedy this scenerio:

1.Right click on #include <string>, and choose &quot;Open Document <string>&quot;
2.Take caution and only change the following lines in the file:

else if (_Tr::eq((_E)_C, _D))
{_Chg = true;
// _I.rdbuf()->snextc();//Comment line out.
//Add line below.
_I.rdbuf()->sbumpc();
break; }

3.getline() should work as expected now.


Mike L.G.
mlg400@blazemail.com
 
Sorry,was in a rush : )

#include <iostream>
#include <string>

using namespace std;

int main()
{
string sFullName;

cout << &quot;Enter full name: &quot;;

getline( cin,sFullName );

cout << sFullName
<< endl;

return 0;

} Mike L.G.
mlg400@blazemail.com
 
Thanks Mike for the info.

Works great. I noticed the string include didn't have a .h to it. I went to put one there and received 7 errors. So I take it some statements don't need the .h extension?

I am curious about the getline statement. Is it possible to use more than one variale in this command seperated by commas, and also conbine couts the same way to compact the code? I think it might work as long as the input and output were stated in an orderly matter?

One more question, someday I would like to make my own win api/guis and make small programs. How far would I have to learn C++ to get to this point of building gui's?

I'm trying hard to learn these oop langs and it can be tough to grasp at times.

Thanks for your time :)

RJ
 
Modern C++ compilers place all the names in headers in the std:: namespace. So you would just #include <iostream>,#include <string> etc...This modern implementation has stripped the .h extension. In older C++ compilers, all names defined in header files were placed in the global namespace, thus the correct syntax would be like this #include <iostream.h> etc...As for your second question, there are many diffent versions of getline() that you can use. You can call it using a C++ string, or use the version that takes an oldschool C-style string as an argument. I don't believe there is an implementation of getline() that coincides with what you stated above. You might want to do some research on other ways you could accomplish that. As for building win32 GUI's, you don't need to know the Object Oriented side of C++ to accomplish this. Of course the MFC tries to insulate the programmer by encapsulating everything ( which would require some knowledge of the OO side of C++) , but to do pure win32 SDK coding, you only need to know the C side of C++. This stems from the fact that the win32 API is coded in C. You will find no classes in the win32 API, just alot of typedefs,structs, and functions. So if you have a solid cornerstone in the above, then you may be ready. Of course raw API programming is a radical departure from the procedural concepts of standard console programming. In the windowed environment, you have to learn the basic concepts of &quot;Event Based Programming&quot;, where you don't execute statment after statment untill you reach the end of main(), but rather code your win32 app to respond to certain events i.e. re-painting the client area, responding to a Timer event,handling a message from a control etc...It's kind of a steep learning curve, but it just takes study and dedication. I would suggest that you study coding a window app with the raw win32 API, then move on to MFC. Getting a firm grasp on creating and maintaining your app via API will enlighten you immensly on the inner-workings of windows, and will make you appreciate the MFC when the time comes to code bigger more complicated apps. Mike L.G.
mlg400@blazemail.com
 
Thanks again Mike.

So C is more of a procedural language and C++ would be pure oop? I'm not clear what mfc is though. I know html, css, and some javascript, these langs are a little harder to grasp but I'm learning very slowly.

I have friends that want me to design their web pages and sites, they think I'm some kind of a genius, lol. If I can learn this C language than I can proudly call myself one, but until then :)

One more question. Is it necessary to learn assembly while doing C? I have looked into assembly and about registers. I have only very minute recollection of this lower language.
I have seen some of the binary and hex, dec calculations, and I can use the calc as a guide for doing binary if need be. I believe I need a compiler for assembly as well?

Thanks again

RJ
 
I would not say that C++ is pure OOP. Unlike some langauges ( JAVA ),C++ gives the programmer the luxury of choosing to go OOP or just procedural. C++ lets the programmer decide what style of programming is best suited for the problem at hand. One thing that can be said about C++, is that it can do everything that C could do, plus a whole lot more. Unfortunatly, you cannot say the same thing about C. You can use C++ as a safer C, but you can also take advantage of C++'s powerful OOP capibilities. It all depends on the problem you are trying to solve. MFC stands for Microsoft Foundation Classes. A long time ago, win32 programmers could only code win32 apps with the SDK. When you code with SDK you are using the raw win32 API to implement your windows. Now this was just fine for short apps that did not have alot of complicated widgets etc...but coding a big project with many dialogs and controls can become very cumbersome if you are using the raw API. In the end, you find yourself writting huge amounts of code. Microsoft's answer was the creation of the MFC. They virtually wrapped every control in a class. As you already may know,OOP can really diminish the complexity,development, and maintainace of big projects. I don't want to compare VisualBasic and VC++, but if you ever coded in VB you realize how easy it is to slap together a GUI. Well, MFC is kind of similar. You visually construct your window, and you drag and drop the controls that you need. Then when you have your GUI looking all nice and neat, you write support code that will make up the logic of each control. MFC really makes big comlicated win32 apps more trivial to code for. But there are some cases where the MFC just gets in the way, where it throws in too much support code, and is totally uneccessary. As your last question concerning assembly, I don't believe that it is a neccessity to learn assembly in conjuncton with C\C++. x86 assembly etc...can give you a speed advantage while giving you a deeper low-level understanding of the machine you are coding for. But with these advantages you sacrifice readibility,development time,faster maintainace and portibility. The only real reason for using assembly over C\C++ is for speed, but often you can always increase speed by improving your algorithms and messing with your compilers optimizing settings. Still you can only gain a better understading of the machine by learning assembly. You can even embed assembly into your C\C++ program. To compile assembly, you need an assembler. MASM is one of the more popular assembers on the windows platform. Mike L.G.
mlg400@blazemail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top