C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 12th, 2005, 09:21 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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>
]

Reply With Quote
  #2  
Old April 13th, 2005, 12:32 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
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.



Reply With Quote
  #3  
Old April 13th, 2005, 07:28 AM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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;
   }

Reply With Quote
  #4  
Old April 13th, 2005, 03:35 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 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";
}
 

Reply With Quote
  #5  
Old April 14th, 2005, 12:03 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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} };

Reply With Quote
  #6  
Old April 17th, 2005, 05:38 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
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...

Reply With Quote
  #7  
Old April 18th, 2005, 02:27 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
*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.

Reply With Quote
  #8  
Old April 18th, 2005, 05:44 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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.

Reply With Quote
  #9  
Old April 18th, 2005, 07:08 PM
Rediahs Rediahs is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 13 Rediahs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 19 m 19 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ newbie - problem is hard to explain


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway