| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
C++ newbie - problem is hard to explain
I'm a real newbie, just started doing C++ today. Okay, so I was trying to get the hang of structures, but I ended up running into other problems. I think I have the structures down pat. But... well... okay. I have no idea how to explain this.
I have a for loop: Code:
for (int n=0; n<4; n++){
cout << fruits[n] << ": " << fruits[n].fPrice << "\n";
}
This is supposed to show four fruits and their prices. The first bit works all right (the names of the fruit) but to get their prices I am having a harder time with this bit: fruits[n].fPrice fruits[n] is an array of strings that has all of the names of the fruits. Each of those fruits are an object (sorry if my terminology is incorrect; I hope you understand what I'm trying to say) and I want the output for this program to be this: apple: .50 orange: .70 melon: .90 grape: 1.00 So when I say fruits[n].fPrice , what I want it to return is apple.fPrice the first time, orange.fPrice the second time... but it doesn't seem to understand that. I tried adding brackets all over the place to specify and I tried using pointers, both to no conclusion. So how do I make this do what I want it to do.. if you can understand what I'm saying? Thanks for reading, I'm probably impossible to understand. ![]() As for the error I'm getting: beginning.cpp(17) : error C2039: 'fPrice' : is not a member of 'std::basic_strin g<_Elem,_Traits,_Ax>' with [ _Elem=char, _Traits=std::char_traits<char>, _Ax=std::allocator<char> ] |
|
#2
|
||||
|
||||
|
could you post all the code, specifically the class and array creating and initializations? I could throw a couple things out there, but I'd rather be able to look at the actual code and see what's wrong
![]()
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
Oh I'm sorry.. here it is. The formatting is a little off for some reason... those spaces aren't supposed to be there (at the top) but I guess that's not a big deal
![]() Code:
#include <string>
#include <iostream>
using namespace std;
int main () {
string fruits[4] = {"apple","orange","grape","melon"};
struct fruit{
float fPrice;
}apple,orange,grape,melon;
apple.fPrice = .85;
orange.fPrice = .95;
grape.fPrice = .50;
melon.fPrice = 2.50;
for (int n=0; n<4; n++){
cout << fruits[n] << ": " << fruits[n].fPrice << "\n";
}
return 0;
}
|
|
#4
|
|||
|
|||
|
First off, you have an array of strings called fruits. This is totaly seperate and independent of your struct fruit. You have created four instances of fruit: apple, orange, grape and melon but they are NOT an array. They are each independent variables and can not be accessed using array notation.
Try something like this: Code:
struct fruit{
string name;
float fPrice;
};
struct fruit fruits[4] = { {"apple", 0.85}, {"orange", 0.95}, {"grape", 0.50}, {"mellon", 2.50} };
...
for (int n=0; n<4; n++) {
cout << fruits[n].name << ": " << fruits[n].fPrice << "\n";
}
|
|
#5
|
|||
|
|||
|
I don't really understand... what exactly are you doing there? And why didn't mine work? I know they are not an array :P there is an array of fruit names and then my structure... where I wanted to use the array to get at the names without having to repeat them.
I see what you've changed it to is better, and it is exactly what I wanted to do, thanks But could you please explain why that works and why mine doesn't?It is this line where I don't understand what's happening: struct fruit fruits[4] = { {"apple", 0.85}, {"orange", 0.95}, {"grape", 0.50}, {"mellon", 2.50} }; |
|
#6
|
|||
|
|||
|
Yours didn't work because fruits and fruit are totaly independent/unique variables.
This line: string fruits[4] = {"apple","orange","grape","melon"}; creates an array of strings called fruits. fruits[0] is assigned the character array (string) "apple". fruits[1] is assigned the character array (string) "orange" fruits[2] is assigned the character array (string) "grape". fruits[3] is assigned the character array (string) "melon". Then you create a struct fruit, with only one element in it: fPrice. Why use as struct if you are only going to have one element in it? Remember the first rulue in programming: KISS - Keep It Simple Stupid You then create four variables of struct fruit called apple, orange, grape and mellon. These are independent variables, they know nothing about each other, or the array fruits and they are not an array and can not be accessed as if they were an array. what I did was to create a struct called fruit wich has two elements in it: a string called name, and a float called fPrice. I then create an array of struct fruit, which I called fruits. The line struct fruit fruits[4] = { {"apple", 0.85}, {"orange", 0.95}, {"grape", 0.50}, {"mellon", 2.50} }; Creates that array and sets fruits[0].name to "apple" fruits[0].fPrice to 0.85 fruits[1].name to "orange" fruits[1].fprice to 0.95 etc. You may have to double check that syntax, as that is not the way I would normally do this. I was just trying to make your code work. This is C++. Why not create a class called fruit with two members; a string called name and float called price. In it's constructor you pass in the name and the price. Code:
class fruit {
private:
string name;
float price;
public:
fruit(const string inName, const float inPrice);
float getPrice();
string getName();
void setPrice(const float inPrice);
}
Or something like this... |
|
#7
|
|||
|
|||
|
*stares blankly* I kept reading that, 5 or more times, and it keeps going way over my head...
I don't know, I'm sorry... I just don't get it.Could you possibly explain in a bit less of a technical way, why an array can't be used to tell the loop to call up each price? Well, I realize that the array has nothing to do with the struct, but I thought that if I use the array's contents, which are named the same as the struct's, that the loop would go through and each time it would replace fruits[n] with its contents, thus it would call to apple.price then orange.price and etc... It's ok if I'm taking up too much of your time You don't have to answer, you've already been very helpful. I'm sorry that I'm a bit slow at this. |
|
#8
|
|||
|
|||
|
all right. take this code (assuming your original code example):
Code:
struct fruit
{
float fPrice;
};
string fruits[2] = { "apple" , "orange" };
fruit apple;
fruit orange;
fruits[0].fPrice = 1.0;
fruits[1].fPrice = 2.0;
now replace fruits[0] and fruits[1] with the strings they are really pointing to: Code:
"apple".fPrice = 1.0; "orange".fPrice = 2.0; now, this does not work. what you want is Code:
apple.fPrice = 1.0; orange.fPrice = 2.0; in C++, strings cannot be interpreted as object names. hope that clears things up a bit! reply if it is still incomprehensible. |
|
#9
|
|||
|
|||
|
Ahhh.. ok thanks... I realize this is not the best way to complete this task, but is there any way that you can turn that string into a thing you can use for a variable name? Or would it be complicated? It's just that, in other languages I used, it allowed you to do these things. But if it doesn't work that way in C++.. hmm, I see. Thanks for all the help guys.
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > C++ newbie - problem is hard to explain |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|