hi i got a problem when try to use pointer and struct..somehow ive manage to compile my coding but the main prob is that the output is not what ive keyed in..

#include <iostream>
#include <conio>
#include <string>
#include <stdlib>

void printtable();
void input();
void calculate ();
void scorecard();
void print();

struct info
{
	char name[40];
   char buggie;
   char dine;
   char membership;
   char side;
   int totalhit;
   int price;
};


main()
{

	printtable();
   input();
   calculate ();
   print();
   system ("pause");
}

void printtable()
{
	string pricetable[3][3]={{"RM 30","RM 40","RM 15"},{"RM 25","RM 30","RM 10"},{"RM 20","RM 25","RM 10"}};

	cout<<"\t\tMember"<<"\t\t"<<"Non"<<"\t\t"<<"Student"<<endl;
	for (int row=0;row<3;row++){
   	if (row==0)
      	cout<<"Play cost\t";
      else if (row==1)
      	cout<<"Buggie\t\t";
      else if (row==2)
      	cout<<"Eat in\t\t";
   	for (int col=0;col<3;col++)
         cout<<pricetable[row][col]<<"\t\t";
         cout<<endl;}

}

void input()
{
	info customer;
	info *anew;
	anew=&customer;

	cout<<"Input Name : ", cin.getline (anew->name,40);
   cout<<"Input Membership type : ", cin>>anew->membership;
   cout<<"Rent Buggie? (Y/N) : ", cin>>anew->buggie;
   cout<<"select side. (E/W) : ", cin>>anew->side;
   cout<<"dine in? (Y/N) : ", cin>>anew->dine;

}

void calculate ()
{
	info customer;
	info *anew;
	anew=&customer;


	if (anew->membership=='m'){
   	if (anew->buggie=='y')
      	if (anew->dine=='y')
         	anew->price=30+25+20;
         else
         	anew->price=30+25;
      else if (anew->buggie=='n')
      	if (anew->dine=='y')
         	anew->price=30+20;
         else
         	anew->price=30;}

   else if (anew->membership=='n'){
   	if (anew->buggie=='y')
      	if (anew->dine=='y')
         	anew->price=40+30+25;
         else
         	anew->price=40+30;
      else if (anew->buggie=='n')
      	if (anew->dine=='y')
         	anew->price=30+25;
         else
         	anew->price=30;}

    else if (anew->membership=='s'){
   	if (anew->buggie=='y')
      	if (anew->dine=='y')
         	anew->price=15+10+10;
         else
         	anew->price=15+10;
      else if (anew->buggie=='n')
      	if (anew->dine=='y')
         	anew->price=15+10;
         else
         	anew->price=15;}
    cout<<anew->price;
    scorecard();
}

void scorecard ()
{
   info customer;
	info *anew;
	anew=&customer;

   int scorecard[9][3],z=0;

	for (int col=0;col<1;col++)
      	for (int row=0;row<9;row++)
         	scorecard[row][col]=z++;

   for (int col=1;col<2;col++){
         	scorecard[0][1]=3;
            scorecard[1][1]=4;
            scorecard[2][1]=3;
            scorecard[3][1]=4;
            scorecard[4][1]=5;
            scorecard[5][1]=3;
            scorecard[6][1]=5;
            scorecard[7][1]=4;
            scorecard[8][1]=4;}

   for (int row=0;row<9;row++){
      	cout<<"input score : ";
      	cin>>scorecard[row][2];
         anew->totalhit=anew->totalhit+scorecard[row][2];}

	cout<<"Hole"<<"\t\t"<<"par"<<"\t\t"<<"player"<<endl;
   for (int row=0;row<9;row++){
   	for (int col=0;col<3;col++)
      	cout<<scorecard[row][col]<<"\t\t";
         cout<<endl;}
}

void print()
{
	info customer;
	info *anew;
	anew=&customer;

   cout<<"Name : "<<anew->name<<endl;
   cout<<"Membership : "<<anew->membership<<endl;
   cout<<"No of Holes played : "<<9<<endl;
   cout<<"Total Hit : "<<anew->totalhit<<endl;
   cout<<"Side Of Course : "<<anew->side<<endl;
   cout<<"transport : "<<anew->buggie<<endl;
   cout<<"Price : "<<anew->price<<endl;
}

sorry for the bad coding..more detail,anew->price, anew->buggie & totalhit in print() function didnt display the value it suppose to display..help, ive been trying to troubleshoot for 3 days now but no luck -_-

Your code definitely compiles and runs, but I'm not quite sure what it's suppose to be doing though, and the output is very cluttered and confusing. I know it has some relation to golf or something.

my lecturer ask me to make any program that involve 2d array, struct, pointer and i/o..right now im stuck with pointer..help -_-"

So you stuck with the print function

you can say that, right now the problem is my totalhit in print () function is that it give wrong value..

Well, the line just below line 138, which is:

anew->totalhit=anew->totalhit+scorecard[row][2]

Try putting that line after the for loop in line 142

nope, just try it still give wrong value..any other suggestion?

What is lines 120 - 122 suppose to do

owh, that line suppose to print out 1st column with number 1-9..

How about this, here's an easy way to input values into your scorecard:

for(int row = 0; row < NumberOfRows; row++)
      for(int col = 0; col < NumberOfColomuns; col++)
            cin >> scorecard[row][col];

thx i get what u mean but need to intialize 1st and 2nd column..only the 3rd column i need to key in data..that why i separate the coding..or are there easier way to do it and still the prob is the final output totalhit in print()..

And here's a way to display it:

for(int row = 0; row < NumberOfRows; row++)
        for(int col = 0; col < NumberOfColoumns; col++)
            cout << scorecard[row][col] << endl;

ur code is to input all the code in every row and column but mine only need to input 3rd column only

Oh, then change col to 3

um, or should be col=2 because start with 0 right..i guess the problem is on how i sum the 3rd column..kinda blur when it involve struct and pointer -_-"

Yes you right col mus be 2, my bad, to sum it do this:

for(row = 0; row < NumberOfRows; row++)
        for(col = 2; col < 3; col++)
           anew->totalhit = anew->totalhit + scorecard[row][2];

huhu, still give wrong value..seriously i dont know what went wrong..

Can't be, I don't see anything wrong with the code I gave, it could be the rest of your code perhaps.

or my coding problem is the pointer itself..-_-"..need an expert to take a look at my coding..
or something went wrong when passing the value to other function..

Could be I'm not too sure about the pointer part but at least you have the gist of it

sirknieghf, you need to learn how local variables work:

void foo()
{
    info customer; // An info object is created

    customer.price = 123; // The info object is modified

} // The info object is destroyed

All of your functions are treating the customer object as if it's magically saved somewhere for reuse, but it's not. You keep recreating an uninitialized info object every single time; naturally the values won't be what you want.

You need to either pass around an info object, or use a global variable, anything that persists the object across the lifetime of your program.

so i need to create global variable for my struct is it? ive try to create global variable earlier but somehow it gives error..i became confuse when struct and pointer are use together..the best part is my lecture note are not whit me so i dont have any reference on how to do it(searching website make me more confuse because they coding style if different)..

one thing i didnt get is that only totalhit gives random value, other display the correct one..

if u dont mind please teach me..as you know im a total newbie on c++ so easy explanation is the best for me..

so i need to create global variable for my struct is it?

You should avoid global variables, but if it simplifies things for now, so be it.

ive try to create global variable earlier but somehow it gives error.

You haven't posted the code, so I can't say what you did wrong.

i became confuse when struct and pointer are use together.

Then don't. Your use of pointers is unnecessary anyway, it's just adding a pointless level of indirection when you could simply touch the customer object directly.

If I were your instructor and asked for a program using structures and pointers, I'd expect something like this:

#include <iostream>

struct info {
    char name[40];
    int price;
};

void input(info *customer);
void print(const info *customer);

int main()
{
    info customer;
    
    input(&customer);
    print(&customer);
}

void input(info *customer)
{
    std::cout << "Name: ";
    std::cin.getline(customer->name, sizeof customer->name);
    std::cout << "Price: ";
    std::cin >> customer->price;
    std::cin.ignore();
}

void print(const info *customer)
{
    std::cout << customer->name << ":\t" << customer->price << '\n';
}

this is my whole program

#include <iostream>
#include <conio>
#include <string>
#include <stdlib>

void printtable();
void input();
void calculate ();
void scorecard();
void print();

struct info
{
	char name[40];
   char buggie;
   char dine;
   char membership;
   char side;
   int totalhit;
   int price;
};


main()
{

	printtable();
   input();
   calculate ();
   print();
   system ("pause");
}

void printtable()
{
	string pricetable[3][3]={{"RM 30","RM 40","RM 15"},{"RM 25","RM 30","RM 10"},{"RM 20","RM 25","RM 10"}};

	cout<<"\t\tMember"<<"\t\t"<<"Non"<<"\t\t"<<"Student"<<endl;
	for (int row=0;row<3;row++){
   	if (row==0)
      	cout<<"Play cost\t";
      else if (row==1)
      	cout<<"Buggie\t\t";
      else if (row==2)
      	cout<<"Eat in\t\t";
   	for (int col=0;col<3;col++)
         cout<<pricetable[row][col]<<"\t\t";
         cout<<endl;}

}

void input()
{
	info customer;
	info *anew;
	anew=&customer;

	cout<<"Input Name : ", cin.getline (anew->name,40);
   cout<<"Input Membership type : ", cin>>anew->membership;
   cout<<"Rent Buggie? (Y/N) : ", cin>>anew->buggie;
   cout<<"select side. (E/W) : ", cin>>anew->side;
   cout<<"dine in? (Y/N) : ", cin>>anew->dine;

}

void calculate ()
{
	info customer;
	info *anew;
	anew=&customer;


	if (anew->membership=='m'){
   	if (anew->buggie=='y')
      	if (anew->dine=='y')
         	anew->price=30+25+20;
         else
         	anew->price=30+25;
      else if (anew->buggie=='n')
      	if (anew->dine=='y')
         	anew->price=30+20;
         else
         	anew->price=30;}

   else if (anew->membership=='n'){
   	if (anew->buggie=='y')
      	if (anew->dine=='y')
         	anew->price=40+30+25;
         else
         	anew->price=40+30;
      else if (anew->buggie=='n')
      	if (anew->dine=='y')
         	anew->price=30+25;
         else
         	anew->price=30;}

    else if (anew->membership=='s'){
   	if (anew->buggie=='y')
      	if (anew->dine=='y')
         	anew->price=15+10+10;
         else
         	anew->price=15+10;
      else if (anew->buggie=='n')
      	if (anew->dine=='y')
         	anew->price=15+10;
         else
         	anew->price=15;}
    cout<<anew->price;
    scorecard();
}

void scorecard ()
{
   info customer;
	info *anew;
	anew=&customer;

   int scorecard[9][3],z=0;

	for (int col=0;col<1;col++)
      	for (int row=0;row<9;row++)
         	scorecard[row][col]=z++;

   for (int col=1;col<2;col++){
         	scorecard[0][1]=3;
            scorecard[1][1]=4;
            scorecard[2][1]=3;
            scorecard[3][1]=4;
            scorecard[4][1]=5;
            scorecard[5][1]=3;
            scorecard[6][1]=5;
            scorecard[7][1]=4;
            scorecard[8][1]=4;}

   for (int row=0;row<9;row++){
      	cout<<"input score : ";
      	cin>>scorecard[row][2];
         anew->totalhit=anew->totalhit+scorecard[row][2];}

	cout<<"Hole"<<"\t\t"<<"par"<<"\t\t"<<"player"<<endl;
   for (int row=0;row<9;row++){
   	for (int col=0;col<3;col++)
      	cout<<scorecard[row][col]<<"\t\t";
         cout<<endl;}
}

void print()
{
	info customer;
	info *anew;
	anew=&customer;

   cout<<"Name : "<<anew->name<<endl;
   cout<<"Membership : "<<anew->membership<<endl;
   cout<<"No of Holes played : "<<9<<endl;
   cout<<"Total Hit : "<<anew->totalhit<<endl;
   cout<<"Side Of Course : "<<anew->side<<endl;
   cout<<"transport : "<<anew->buggie<<endl;
   cout<<"Price : "<<anew->price<<endl;
}

i see, so i should include pointer in function not to the struct? it seems much easier when doing that..ill try ur method, any oher tips?

omg, thank you..i cant believe how simple it is by using ur example..

another question, how do i intergrate i/o..im planning to save the customer info in notepad..

heres my new code..

#include <iostream>
#include <conio>
#include <string>
#include <stdlib>


struct info
{
	char name[40];
   char buggie;
   char dine;
   char membership;
   char side;
   int price;
   int totalhit;
};

void printtable();
void input(info *customer);
void calculate (info *customer);
void scorecard(info *customer);
void print(info *customer);


main()
{
	info customer;

	printtable();
   input(&customer);
   calculate(&customer);
   scorecard(&customer);
   print(&customer);
   system ("pause");
}

void printtable()
{
	string pricetable[3][3]={{"RM 30","RM 40","RM 15"},{"RM 25","RM 30","RM 10"},{"RM 20","RM 25","RM 10"}};

	cout<<"\t\tMember"<<"\t\t"<<"Non"<<"\t\t"<<"Student"<<endl;
	for (int row=0;row<3;row++){
   	if (row==0)
      	cout<<"Play cost\t";
      else if (row==1)
      	cout<<"Buggie\t\t";
      else if (row==2)
      	cout<<"Eat in\t\t";
   	for (int col=0;col<3;col++)
         cout<<pricetable[row][col]<<"\t\t";
         cout<<endl;}

      cout<<"5% tax for every costumer"<<endl;
}

void input(info *customer)
{

	cout<<"Input Name : ", cin.getline (customer->name,40);
   cout<<"Input Membership type : ", cin>>customer->membership;
   cout<<"Rent Buggie? (Y/N) : ", cin>>customer->buggie;
   cout<<"select side. (E/W) : ", cin>>customer->side;
   cout<<"dine in? (Y/N) : ", cin>>customer->dine;

}

void calculate (info *customer)
{

	if (customer->membership=='m'){
   	if (customer->buggie=='y')
      	if (customer->dine=='y')
         	customer->price=30+25+20;
         else
         	customer->price=30+25;
      else if (customer->buggie=='n')
      	if (customer->dine=='y')
         	customer->price=30+20;
         else
         	customer->price=30;}

   else if (customer->membership=='n'){
   	if (customer->buggie=='y')
      	if (customer->dine=='y')
         	customer->price=40+30+25;
         else
         	customer->price=40+30;
      else if (customer->buggie=='n')
      	if (customer->dine=='y')
         	customer->price=30+25;
         else
         	customer->price=30;}

    else if (customer->membership=='s'){
   	if (customer->buggie=='y')
      	if (customer->dine=='y')
         	customer->price=15+10+10;
         else
         	customer->price=15+10;
      else if (customer->buggie=='n')
      	if (customer->dine=='y')
         	customer->price=15+10;
         else
         	customer->price=15;}
    cout<<customer->price;
}

void scorecard (info *customer)
{

   int scorecard[9][3],z=0, hit=0;

	for (int col=0;col<1;col++)
      	for (int row=0;row<9;row++)
         	scorecard[row][col]=z++;

   for (int col=1;col<2;col++){
         	scorecard[0][1]=3;
            scorecard[1][1]=4;
            scorecard[2][1]=3;
            scorecard[3][1]=4;
            scorecard[4][1]=5;
            scorecard[5][1]=3;
            scorecard[6][1]=5;
            scorecard[7][1]=4;
            scorecard[8][1]=4;}

   for (int row=0;row<9;row++){
      	cout<<"input score : ";
      	cin>>scorecard[row][2];}

   for(int row = 0; row<9; row++)
   		customer->totalhit = customer->totalhit + scorecard[row][2];

	cout<<"Hole"<<"\t\t"<<"par"<<"\t\t"<<"player"<<endl;
   for (int row=0;row<9;row++){
   	for (int col=0;col<3;col++)
      	cout<<scorecard[row][col]<<"\t\t";
         cout<<endl;}

}

void print(info *customer)
{

	cout<<"Name : "<<customer->name<<endl;
   cout<<"Membership : "<<customer->membership<<endl;
   cout<<"No of Holes played : "<<9<<endl;
   cout<<"Total Hit : "<<customer->totalhit<<endl;
   cout<<"Side Of Course : "<<customer->side<<endl;
   cout<<"transport : "<<customer->buggie<<endl;
   cout<<"Price : "<<customer->price<<endl;
}

btw may i know the purpose you use std::... and what is the purpose of std::cin.ignore()

If you don't mind me asking, what compiler are you using, and what version? The console I/O library isn't a standard one (which is fortunate, as you don't actually use it and can remove it from the include list), and the modern C++ header for the standard library is <cstdlib> , not <stdlib> . Also, most of the newer IDEs (including Code::Blocks and the newer versions of Visual C++) all pause after the end of a console program automatically, obviating the need for the system() call (and in turn the include for the standard library). Finally, any newer C++ compiler would require you to use the std namespace for the <iostream> and <string> objects and functions.

thanks for the info..im using borland c++ 5.02..

If you have any choice in the matter (and I realize you might not), I would suggest getting a more modern compiler and IDE. I personally find Code::Blocks to be very good, though Visual C++ Express has a superior debugger. If all else fails, and you can afford to pay for it, C++ Builder XE2 is the modern descendant of the Borland compiler. Given that you are writing what is essentially standard C++ code, it should compile and run under any of these, so which to use boils down to personal preference.

yeah ive notice mine are ancient but i cant change right now, you guess right..cause they teaching using borland 5.02 so i have to stick with it..next semester is java, i dont know how im gonna keep up even borland i have hard time..

back to the question can you teach me easy way to integrate i/o for my coding..im panning to make i/o file for the struct

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.