• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ Noobie Question

CoBRaXT

Golden Member
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.
 
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;
 
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)
 
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...
 
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.
 
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
 
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?
 
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.
 
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;
}
 
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.
 
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.
 
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.
 
Back
Top