Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-06-2004, 01:03 PM   #1 (permalink)
Crow T. Robot
Registered User
 
Join Date: Nov 2004
Posts: 14
Crow T. Robot is on a distinguished road
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!"
    }
Crow T. Robot is offline   Reply With Quote
Old 12-07-2004, 06:48 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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!"
    }
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 12-07-2004, 07:05 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Red, when do you stop mixing std C++ with C?
Code:
cin.getline(name);
to
Code:
getline(cin, name);
__________________
Valmont is offline   Reply With Quote
Old 12-08-2004, 06:47 PM   #4 (permalink)
Rotkiv
Code Monkey
 
Rotkiv's Avatar
 
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 64
Rotkiv is on a distinguished road
Send a message via AIM to Rotkiv Send a message via MSN to Rotkiv
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?
Rotkiv is offline   Reply With Quote
Old 12-09-2004, 02:10 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
Originally Posted by Valmont
Red, when do you stop mixing std C++ with C?
Code:
cin.getline(name);
to
Code:
getline(cin, name);
Sorry, can't help it, I started out with strict C, never seemed to get past it...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 12-09-2004, 02:14 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 12-09-2004, 05:01 AM   #7 (permalink)
Kernel_Killer
Regular Contributor
 
Kernel_Killer's Avatar
 
Join Date: Feb 2003
Location: indisclosed
Posts: 210
Kernel_Killer is on a distinguished road
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;
__________________
Network Synapse
Screaming Electron
Kernel_Killer is offline   Reply With Quote
Old 12-09-2004, 10:51 AM   #8 (permalink)
Rotkiv
Code Monkey
 
Rotkiv's Avatar
 
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 64
Rotkiv is on a distinguished road
Send a message via AIM to Rotkiv Send a message via MSN to Rotkiv
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;'
Rotkiv is offline   Reply With Quote
Old 12-10-2004, 05:51 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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...)
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB or C#, or something else Apodysophilia MS Technologies ( ASP, VB, C#, .NET ) 6 10-15-2004 10:48 AM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 03:36 AM
Basic, not visual platjyjim All Other Coding Languages 1 03-01-2003 10:00 AM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 06:56 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting