Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,529 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,098 people online right now. Registration is fast and FREE... Join Now!




structure

2 Pages V  1 2 >  
Reply to this topicStart new topic

structure

rollinhard
post 20 Nov, 2005 - 04:22 PM
Post #1


D.I.C Head

**
Joined: 13 Feb, 2005
Posts: 60


My Contributions


ok i'm having quite a few problems with this structure program.
first i'm gonna explain what i have to do. i need to have a structure
1. store the name of stock, estimated earnings per share, estimated price-to-earnings ratio
2. have program prompt the user to enter these for 5 stocks using the same structure to store the data.
3. when data is entered have program compute and display stock price used by multiplying the entered earnings and price per share

here is what i have so far
CODE

#include <iostream>
#include <string>
using namespace std;      
 
struct stocks  
{
  string stockNname;
  float earningPerShare;
  float priceToEarning;
};  

void populate(stocks *);
void dispOne(stocks *);

int main()
{
   char key;
   stocks *recPoint
   
   cout << "Do you want to enter the stock name? (y for yes n for no): ";
   key = cin.get();
   if (key == 'y')
{
   key = cin.get();
   recPoint= new stock;
   populate(recPoint);
   dispOne(recPoint);
   else
   cout <<"\nNo record has been created";
   return 0;
}
void populate(stock *record)
{
    cout << "Enter the stock name: ";
    getline(cin, record->stockName);
    cout << "Enter the Earnings Per Share";
    getline(cin, record->earningPerShare);
    cout << "Enter the Price To Earning Ratio";
    getline(cin, record->priceToEarning;

return;
}


i know i need to add something like this for the 5 things to be entered
CODE

const int NUMRECS = 5;
 int i, something here[NUMRECS ], total=0;
 for (i=0; i < NUMRECS; i++)
 {
   cout << "enter something here\n";
   cin >> something here[i];
 }    

how do i incorporate this?

This post has been edited by rollinhard: 20 Nov, 2005 - 04:23 PM
User is offlineProfile CardPM

Go to the top of the page


Mrafcho001
post 20 Nov, 2005 - 05:03 PM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


you can always make an array of stocks.

stock sStocks[5];

for(int i = 0; i < 5; i++)
{
cout << "Enter Stock Name: ";
cin >> sStock[i].stockNname;
cout << "Enter Earnings per Share: ";
cin >> sStock[i].earningPerShare;
cout << "Enter Price to Earnings Ratio: ";
cin >> sStock[i].priceToEarning;
}


also in main


if (key == 'y')
{
key = cin.get();
recPoint= new stock;
populate(recPoint);
dispOne(recPoint);
} //forgot a closing bracket ?
else
cout <<"\nNo record has been created";

return 0;
}


This post has been edited by Mrafcho001: 20 Nov, 2005 - 05:05 PM
User is offlineProfile CardPM

Go to the top of the page

rollinhard
post 21 Nov, 2005 - 08:43 AM
Post #3


D.I.C Head

**
Joined: 13 Feb, 2005
Posts: 60


My Contributions


ok so i have this now
i am getting an error on line 53 expected primary-expression before "double" what am i missing
CODE

#include <iostream>
#include <string>
using namespace std;      
 
struct stock  
{
  string stockName;
  double earningPerShare;
  double priceToEarning;
};  

void populate(stock *);
void dispOne(stock *);
double calculateStock;

int main()
{
   char key;
   stock *recPoint;
   
   cout << "Do you want to enter the stock name? (y for yes n for no): ";
   key = cin.get();
   if (key == 'y')
{
   key = cin.get();
   recPoint= new stock;
   populate(recPoint);
   dispOne(recPoint);
}
   else
   cout <<"\nNo record has been created";
   return 0;


void populate(stock *record);

    stock NEWRECS[5];

for(int i = 0; i < 5; i++)
{
cout << "Enter Stock Name: ";
cin >> NEWRECS[i].stockName;
cout << "Enter Earnings per Share: ";
cin >> NEWRECS[i].earningPerShare;
cout << "Enter Price to Earnings Ratio: ";
cin >> NEWRECS[i].priceToEarning;
}

double calculateStock = (double earningPerShare * double priceToEarning)

return (calculateStock);
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 21 Nov, 2005 - 08:54 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


Is this
CODE

void populate(stock *record);

   stock NEWRECS[5];

for(int i = 0; i < 5; i++)
{
cout << "Enter Stock Name: ";
cin >> NEWRECS[i].stockName;
cout << "Enter Earnings per Share: ";
cin >> NEWRECS[i].earningPerShare;
cout << "Enter Price to Earnings Ratio: ";
cin >> NEWRECS[i].priceToEarning;
}

double calculateStock = (double earningPerShare * double priceToEarning)

return (calculateStock);
}

section of your code supposed to represent calls to functions? Function definitions?
User is offlineProfile CardPM

Go to the top of the page

rollinhard
post 21 Nov, 2005 - 08:56 AM
Post #5


D.I.C Head

**
Joined: 13 Feb, 2005
Posts: 60


My Contributions


that is where i am supposed to have the user input 5 different stocks, after being asked if they want to .. thats where i was having the first problem...i tried using the array example from above...that info is supposed to be stored

This post has been edited by rollinhard: 21 Nov, 2005 - 08:57 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 21 Nov, 2005 - 09:02 AM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


Well, you are storing the data in the array of structures, but you are then trying to perform a multiplication on a set of variables that are avaiable in each of those structures...you will have to specify each one.

You are then returning the value, but are not actually returning it to anywhere is the main function. Also, if this is a separate function, it must be outside the main function...it is currently inside.
User is offlineProfile CardPM

Go to the top of the page

rollinhard
post 21 Nov, 2005 - 09:09 AM
Post #7


D.I.C Head

**
Joined: 13 Feb, 2005
Posts: 60


My Contributions


so i need to move the multiplication outside the main and write the operation 5 times? and do a cout << to display the calculation right

This post has been edited by rollinhard: 21 Nov, 2005 - 09:10 AM
User is offlineProfile CardPM

Go to the top of the page

amos
post 21 Nov, 2005 - 09:15 AM
Post #8


D.I.C Head

**
Joined: 16 Apr, 2005
Posts: 142


My Contributions


CODE
   else
  cout <<"\nNo record has been created";
  return 0;


void populate(stock *record);

   stock NEWRECS[5];

for(int i = 0; i < 5; i++)
{
cout << "Enter Stock Name: ";
cin >> NEWRECS[i].stockName;
cout << "Enter Earnings per Share: ";
cin >> NEWRECS[i].earningPerShare;
cout << "Enter Price to Earnings Ratio: ";
cin >> NEWRECS[i].priceToEarning;
}

double calculateStock = (double earningPerShare * double priceToEarning)

return (calculateStock);
}


Try altering this part to:


CODE
   else
  cout <<"\nNo record has been created";
  return 0;
}//end of main (This is a comment by the way only the bracket is required)

void populate(stock *record) //Note no semi colon.
{ //Note opening bracket.
   stock NEWRECS[5];

for(int i = 0; i < 5; i++)
{
cout << "Enter Stock Name: ";
cin >> NEWRECS[i].stockName;
cout << "Enter Earnings per Share: ";
cin >> NEWRECS[i].earningPerShare;
cout << "Enter Price to Earnings Ratio: ";
cin >> NEWRECS[i].priceToEarning;
}

double calculateStock = (double earningPerShare * double priceToEarning)

return (calculateStock);
}


And see if that does it.

Cheers
Amos
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 21 Nov, 2005 - 09:20 AM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


Try something like this, as far as functionality goes...you can enhance based on your requirements:
CODE

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

struct stock  
{
 string stockName;
 double earningPerShare;
 double priceToEarning;
};  



int main()
{
  stock myStocks[5];
  for(int i=0;i<5;i++)
  {
     cout<<"Please enter the name for stock "<<i<<endl;
     cin>>myStocks[i].stockName;
     cout<<"Please enter the earningPerShare for stock "<<i<<endl;
     cin>>myStocks[i].earningPerShare;
     cout<<"Please enter the priceToEarning for stock "<<i<<endl;
     cin>>myStocks[i].priceToEarning;
  }
  for(i=0;i<5;i++)
  {
     cout<<"The stock price for stock "<<i<<" is "<<myStocks[i].earningPerShare*myStocks[i].priceToEarning<<endl;
  }
  return 0;
}
User is offlineProfile CardPM

Go to the top of the page

rollinhard
post 21 Nov, 2005 - 09:22 AM
Post #10


D.I.C Head

**
Joined: 13 Feb, 2005
Posts: 60


My Contributions


ok so i changed to that up now the only error i am getting is on line 54
it still says expected primary expression...any ideas
CODE

#include <iostream>
#include <string>
using namespace std;      
 
struct stock  
{
  string stockName;
  double earningPerShare;
  double priceToEarning;
};  

void populate(stock *);
void dispOne(stock *);
double calculateStock;

int main()
{
   char key;
   stock *recPoint;
   
   cout << "Do you want to enter the stock name? (y for yes n for no): ";
   key = cin.get();
   if (key == 'y')
{
   key = cin.get();
   recPoint= new stock;
   populate(recPoint);
   dispOne(recPoint);
}
 
  else
 cout <<"\nNo record has been created";
 return 0;
}

void populate(stock *record)
{
  stock NEWRECS[5];

for(int i = 0; i < 5; i++)
{
cout << "Enter Stock Name: ";
cin >> NEWRECS[i].stockName;
cout << "Enter Earnings per Share: ";
cin >> NEWRECS[i].earningPerShare;
cout << "Enter Price to Earnings Ratio: ";
cin >> NEWRECS[i].priceToEarning;
}

double calculateStock = (double earningPerShare * double priceToEarning)

return (calculateStock);
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 21 Nov, 2005 - 09:26 AM
Post #11


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 31 times

Dream Kudos: 25
My Contributions


What line is 54?

You are still multiplying aggregated structure values instead of accessing them one by one. See my post above.
User is offlineProfile CardPM

Go to the top of the page

rollinhard
post 21 Nov, 2005 - 09:36 AM
Post #12


D.I.C Head

**
Joined: 13 Feb, 2005
Posts: 60


My Contributions


sorry i know i am sucking at this quite a bit. here is what i have now
CODE

#include <iostream>
#include <string>
using namespace std;      
 
struct stock  
{
  string stockName;
  double earningPerShare;
  double priceToEarning;
};  

void populate(stock *);
void dispOne(stock *);
double calculateStock;

int main()
{
 stock myStocks[5];
 for(int i=0;i<5;i++)
 {
    cout<<"Please enter the name for stock "<<i<<endl;
    cin>>myStocks[i].stockName;
    cout<<"Please enter the Earning Per Share for the stock "<<i<<endl;
    cin>>myStocks[i].earningPerShare;
    cout<<"Please enter the Price To Earning for the stock "<<i<<endl;
    cin>>myStocks[i].priceToEarning;
 }
 for(i=0;i<5;i++)
 {
    cout<<"The stock price for stock "<<i<<" is "<<myStocks[i].earningPerShare*myStocks[i].priceToEarning<<endl;
 }
 return 0;


i am getting an error on
CODE
 for(i=0;i<5;i++)

it says
QUOTE
name lookup of `i' changed for new ISO `for' scoping
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/20/08 01:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month