C++ Noobie Question

CoBRaXT

Golden Member
Mar 11, 2002
1,241
0
76
How do I identify something as a string? I have to make a menu where the user picks between 4 entrees, 2 toppings for each entre, and 8 beverages.
I want to store, for example, x as the word "pasta" so later I can type

cout << "Your entre is: " << x << endl;

thanks out there.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
you have two choices, a character array or std::string. i suggest you use std::string at first. to use it, include <string> (no .h!) then put using namespace std; after the #includes

then you can just declare a string by doing:
string x;
 

CoBRaXT

Golden Member
Mar 11, 2002
1,241
0
76
Originally posted by: dighn
you have two choices, a character array or std::string. i suggest you use std::string at first. to use it, include <string> (no .h!) then put using namespace std; after the #includes

then you can just declare a string by doing:
string x;

that worked but when i call upon the string later:

cout << "Entree is " << x << endl;

it gives me this error:

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
what compiler do you have? btw you should be able to get around that problem by doing << x.c_str() but then that's not as good...
 

pennylane

Diamond Member
Apr 28, 2002
6,077
1
0
I thought it was apstring blahblah
where you have to #include<apstring.h> or something
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: fanerman91
I thought it was apstring blahblah
where you have to #include<apstring.h> or something

apstring is non-standard. it's only used in ap comp sci or something.
 

CoBRaXT

Golden Member
Mar 11, 2002
1,241
0
76
Originally posted by: dighn
what compiler do you have? btw you should be able to get around that problem by doing << x.c_str() but then that's not as good...

visual c++ 6.0...and this is supposed to be a basic assignment since we haven't learned anything even remotely close to that yet.
is there any other way, (we are supposed to just used nested if statements) to allow the user to pick an entree, say pasta, store that to memory and then display it at the end along with the toppings and drink too?

thanks
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: CoBRaXT
Originally posted by: dighn
what compiler do you have? btw you should be able to get around that problem by doing << x.c_str() but then that's not as good...

visual c++ 6.0...and this is supposed to be a basic assignment since we haven't learned anything even remotely close to that yet.
is there any other way, (we are supposed to just used nested if statements) to allow the user to pick an entree, say pasta, store that to memory and then display it at the end along with the toppings and drink too?

thanks

strange... it's supposed to work. <string> should include the correct << operator. you didn't by any chance include string.h did you?

anyway how about using numbers for picking choices?
 

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
visual c++ does have a problem with the include<string>. That is the reason why i don't use visual c++. Try on a g++ compiler, it shouldn't have any problems.
 

ProviaFan

Lifer
Mar 17, 2001
14,993
1
0
Originally posted by: CoBRaXT
Originally posted by: dighn
what compiler do you have? btw you should be able to get around that problem by doing << x.c_str() but then that's not as good...

visual c++ 6.0...and this is supposed to be a basic assignment since we haven't learned anything even remotely close to that yet.
is there any other way, (we are supposed to just used nested if statements) to allow the user to pick an entree, say pasta, store that to memory and then display it at the end along with the toppings and drink too?

thanks
Are you supposed to use strings for this? I'm thinking that if this is a simpler assignment, maybe you should be approaching this with an entirely different method. Try storing each option as a number in an int, and when it comes time to print, do something like this:
switch ( entree ) {
case 1:
cout << "First item" << endl;
break;

case 2:
cout << "Second item" << endl;
break;

case 3:
cout << "Third item" << endl;
break;

default:
cout << "Unrecognized item" << endl;
break;
}
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
Originally posted by: CoBRaXT
How do I identify something as a string? I have to make a menu where the user picks between 4 entrees, 2 toppings for each entre, and 8 beverages.
I want to store, for example, x as the word "pasta" so later I can type

cout << "Your entre is: " << x << endl;

thanks out there.

wtf are you guys talking about?

just declare:

char x[20]; // 20 characters

then you can do
cin >> x;
cout << "Your entre is: " << x << endl;

oops if you don't want to use cin you can declare it like this:
char x[] = "pasta";
or you can do what the post above me suggested which would be much smarter solution to your assignment.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
You need to include <iostream> as well as <string>

#include <string>
#include <iostream>
using namespace std;

should be in the begining of most of your console programs.
 

Cal166

Diamond Member
May 6, 2000
5,081
8
81
Originally posted by: glugglug
You need to include <iostream> as well as <string>

#include <string>
#include <iostream>
using namespace std;

should be in the begining of most of your console programs.

This is the first thing they teach you in the course. ALWAYS USE THESE LIBRARIES.