|
 |
|
 |
12-06-2004, 12:03 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Nov 2004
Posts: 14
|
Basic C++ functionality
I'm familiar with Java but have just started C++. I have a very simple program but need to work out mostly problems such as syntax, proper declarations, etc. The program simply asks the user for their name, a lucky number, and then print them out, but I don't think I have variables initialized or read in properly, and probably some other problems of that nature. I just started using a C++ compiler and don't know how to get it to actually compile and run the program, so here it is.
Code:
#include <iostream>
int main()
{
char name;
int num;
cout << "Enter your name/n";
cin >> name;
cout << "Enter your favorite lucky number/n";
cin >> num;
cout << "Hello " + name + ", I like the number " + num + "also!"
}
|
|
|
12-07-2004, 05:48 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int num;
cout << "Enter your name/n";
cin.getline(name);
cout << "Enter your favorite lucky number/n";
cin >> num;
cout << "Hello " + name + ", I like the number " + num + "also!"
}
|
|
|
12-07-2004, 06:05 AM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Red, when do you stop mixing std C++ with C?
to
Code:
getline(cin, name);
__________________
|
|
|
12-08-2004, 05:47 PM
|
#4 (permalink)
|
|
Code Monkey
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 59
|
all yalls posts confused me
all you have to do is add:
std::
to the front of all of the cin's and cout's and your good to go.
redhead, I'm not questioning your knowledge but rather wanting to tap it:
what's the advantage of using string rather than char?
|
|
|
12-09-2004, 01:10 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Quote:
|
Originally Posted by Valmont
Red, when do you stop mixing std C++ with C?
to
Code:
getline(cin, name);
|
Sorry, can't help it, I started out with strict C, never seemed to get past it...
|
|
|
12-09-2004, 01:14 AM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Quote:
|
Originally Posted by Rotkiv
redhead, I'm not questioning your knowledge but rather wanting to tap it:
what's the advantage of using string rather than char?
|
In his example he's only allocating room for one single char, not a name consisting of several chars.. Strange things might happen with a program like that, unless the std::cin(char) operator has been modified to dynamicaly allocate whatever is needed. Which i doubt, but I havn't seen the current deffinition of the standard function.
By using string, theres anough room, since string class will dynamicaly allocate space, and there are several usefull features hidden in there aswell.
|
|
|
12-09-2004, 04:01 AM
|
#7 (permalink)
|
|
Regular Contributor
Join Date: Feb 2003
Location: indisclosed
Posts: 210
|
Quote:
all yalls posts confused me
all you have to do is add:
std::
to the front of all of the cin's and cout's and your good to go.
|
You don't need them since he added the namespace.
Code:
using namespace std;
|
|
|
12-09-2004, 09:51 AM
|
#8 (permalink)
|
|
Code Monkey
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 59
|
thanx red.
kernel_killer:
I was editing the original code. I was told on these forums by valmont or red or someone that using std:: was better in the long run, if you plan on getting into namespaces, then 'using namespace std;'
|
|
|
12-10-2004, 04:51 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Quote:
|
Originally Posted by Rotkiv
thanx red.
|
You're welcome
Quote:
|
Originally Posted by Rotkiv
I was editing the original code. I was told on these forums by valmont or red or someone that using std:: was better in the long run, if you plan on getting into namespaces, then 'using namespace std;'
|
That is true, if you plan on having alot of members writing/editing on your code, then the std:: way is better, since you know for sure its the right namespace you're dealing with, than just asuming the "using namespace std" will apply when it reaches your part of the code, if some one else plan on changing that above your code segment.
(don't know if I express myself clear enough here...)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 12:02 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|