Saturday, May 22, 2010

C++ beginner...?

i just got into programming and i have a cheap c++ compiler and i was playing around with it...i was wondering if i make a program that asks the question "what is your name?" is there a way to input more than one letter or variable for the answer because from what i have found i can only use multiple int's and not char's. any tips? keep in mind im just a beginner so if i sound stupid its because i just started messing with it like 10 min ago. thanks for your help

C++ beginner...?
Get a better compiler. I just had fun with this program. I compiled it on Linux using gcc and I'll show you the output in a minute.





#include %26lt;iostream%26gt;


#include %26lt;cstdio%26gt;


using namespace std;





int main(){





char Name[30];


char OtherName[30];





cout %26lt;%26lt; "Enter your name:";


cin %26gt;%26gt; Name;


cout %26lt;%26lt; "You entered " %26lt;%26lt; Name %26lt;%26lt; endl;


cout %26lt;%26lt; "Enter your name:";


gets(OtherName);


cout %26lt;%26lt; "You entered " %26lt;%26lt; OtherName %26lt;%26lt; endl;


return 0;


}








Here is what compiling and running it looks like:





jplatt@darkstar:~/Feb17$ g++ -o Name Name.cc


/tmp/cczWw2n0.o: In function `main':


Name.cc:(.text+0x204): warning: the `gets' function is dangerous and should not be used.





jplatt@darkstar:~/Feb17$ ./Name


Enter your name:John D. L. Platt


You entered John


Enter your name:You entered D. L. Platt





jplatt@darkstar:~/Feb17$





In other words, when I used cin %26gt;%26gt; Name it took everything I entered up to the first space and put it in name. There was more but it took it ALL and put in into OtherName. Happens. Your compiler should be able to do that if it's c++, not C.





Mind, GCC is not Linux-specific. In fact, my first experience with it was in 1987 when I was a word processor doing work for various engineering firms around RI which ran Unix. Linux came along in 1991. It's been ported to just about every OS which has come along since then. If you are on Windows the cheapest and least resource hungry is available for free download from http://www.delorie.com/djgpp/zip-picker....





There is an alternative: this is where one version of Linux shines. I'm talking about the livecd. You can download an iso file for free and burn it to a CD with nero or some other software. Knoppix was created by a computer consultant in Germany for use with his consultancy. It gives him a portable desktop to carry around with his favorite applications and the ability, should he choose to do so, This basically means, you burn it to a CD, set a computer to boot from the CD, start it up and you have a running Linux system in memory -- it saves nothing to disk unless specifically told to in a cumbersome manner-- and you can type in, compile and run C and C++ programs using one of the most sophisticated compilers out there, without having to go to the trouble of installing it. More information is at:


http://www.knoppix.net/wiki/Main_Page





You can do the same thing with Damn Small Linux which has among other advantages the iso file is much smaller and if you can get your machine to boot from a pen drive you can load it onto a very affordable one. More information is at:


http://damnsmalllinux.org/





I could fix the program above. Bluntly, I would prefer gets() or cin.getline for input but the first step is making sure you can compile one of these programs. If you can't get a new compiler. And hey, GCC has been around for twenty years now.
Reply:Look at the string class and cin.
Reply:i can help you with it, my msn is amwpessy@hotmail.com,or you can give me a email ------fadewolf@qq.com
Reply:/*I suggest to use Borland c++ compiler or turbo c++ compiler*/


/*Answer of your question is by using string method*/


/*see the following code*/


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;string.h%26gt;


void main()


{


char name[100];


printf("Enter your name ");


gets(name);


/*Then write any process you want*/


getch();


}


No comments:

Post a Comment