PDA

View Full Version : Basic C++ Problems


waternorth
10-06-2004, 11:56 PM
I'm having basic C++ problems. What's the problem? I'll do a direct copy+paste from Command Prompt.

Hello, please input your username:
Username:Test //I typed this in



Welcome! Please choose a category to enter.




Cat01
Cat02

// Does not prompt me to type anything in and just shows the following message.

Welcome to Cat1


Source:

#include <iostream.h>

int main ()
{
cout << "Hello, please input your name: \nName:";
int name;
cin >> name;
cout << "\n \n \n";
cout << "Welcome! Please choose a category to enter. \n \n \n \n \n Cat01 \n Cat02 \n \n";
int cat;
cin >> cat;
if (cat == 1)
then
cout << "Welcome to Cat1";
if (cat == 2)
then
cout << "Welcome to Cat2";
}

Can anyone tell me what I'm doing wrong?

Muuss
10-07-2004, 12:01 AM
int -> integer, this is a number, not a char string.
name will never contain 'Test'.

After than check the if syntax :)

waternorth
10-07-2004, 12:03 AM
Soo.... what do I use instead of int?

Muuss
10-07-2004, 12:09 AM
char name[12];
for example

if (cat==1) {
}

instead of
if (cat==1) then

waternorth
10-07-2004, 12:16 AM
Revised:


#include <iostream.h>

int main ()
{
cout << "Hello, please input your name: \nName:";
char name;
cin >> name;
cout << "\n \n \n";
cout << "Welcome! Please choose a category to enter. \n \n \n \n \n Cat01 \n Cat02 \n \n";
char cat;
cin >> cat;
if (cat==1)
cout << "Welcome to Cat1";
if (cat==2)
cout << "Welcome to Cat2";
return 1;
}

It now asks for the input. BUT it does not say "Welcome to Cat#"

Muuss
10-07-2004, 12:19 AM
keep cat as an int if you make your tests like that " if (cat==1) "
1 is an int
'1' is a char

Scorpious2k
10-07-2004, 12:27 AM
Muuss is right.

You made cat a type char so the compare should be:

if (cat=='1')
cout << "Welcome to Cat1";
if (cat=='2')
cout << "Welcome to Cat2";

Otherwise you are testing for ctrl+A or ctrl+B.

jbb
10-07-2004, 01:00 AM
How to read a string properly in C++.


#include <iostream>
#include <string>

using namespace std;

int main()
{
string name;
getline(cin, name);
cout << name << "\n";
}