help with my C++ hw please??? should be easy for u guys

Smokey0066

Senior member
Oct 9, 1999
488
0
0
okay heres what i'm trying to do. I define a struct and then call a function to write to the struct then call another function to display whats in the struct.

Problem: i dont know how to reference struct in the function prototype; maybe i'm looking at this the wrong way.. anyone wanna straighten me out??

thanks.

CODE:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

void readvalue(_____);
void showvalue(_____);

struct Quad
{
double a; double b; double c;
};
Quad read;
int main()
{
Quad read;

readvalue(_____);

showvalue(_____);

}

void readvalue(_____)
{
double w, e, r;
cout<<"Enter the coefficients a, b, c: ";
cin>>w >>e >>r;
cout<<w <<e <<r <<endl;
Quad read={w, e, r};
}

void showvalue(_____)
{
cout<<"The values entered are: " <<endl <<read.a <<endl <<read.b <<endl <<read.c <<endl;

}
 

Smokey0066

Senior member
Oct 9, 1999
488
0
0
forgot to mention that the functions should only have references to my Quad struct.

the more i think the more frustrated i get... GRRRR
 

GiLtY

Golden Member
Sep 10, 2000
1,487
1
0
Well im not fluent with C, but it seems to me that the most reasonable thing to do would be declare the struc frist, so the parameter list in your function prototype would be able to recognize any pointer of the struct type
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
I thought the assignment was in C? Why are you using C++? Here it is in C:

#include <stdio.h>


typedef struct QuadType
{
double a;
double b;
double c;
}Quad;



void ReadQuad(Quad *pQ);
void WriteQuad(Quad *pQ);



int main()
{
Quad quad;
ReadQuad(&quad);
WriteQuad(&quad);

return 0;
}



void ReadQuad(Quad *pQ)
{
printf("Enter co-efficients a, b, c: ");
scanf("%lf %lf %lf", &pQ->a, &pQ->b, &pQ->c);
}

void WriteQuad(Quad *pQ)
{
printf("\nCo-efficients are:\n");
printf("a = %f\n", pQ->a);
printf("b = %f\n", pQ->b);
printf("c = %f\n", pQ->c);
}


 

Smokey0066

Senior member
Oct 9, 1999
488
0
0
sorry.. c++ i thought you'd know from the code..

can you tell me how to do it in c++ thanks BTW
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: Smokey0066
sorry.. c++ i thought you'd know from the code..

can you tell me how to do it in c++ thanks BTW

Here ya go:

#include <iostream.h>


struct Quad
{
double a;
double b;
double c;
};



void ReadQuad(Quad &q);
void WriteQuad(Quad &q);



int main()
{
Quad quad;
ReadQuad(quad);
WriteQuad(quad);

return 0;
}



void ReadQuad(Quad &q)
{
cout << "Enter co-efficients a, b, and c: ";
cin >> q.a >> q.b >> q.c;
}

void WriteQuad(Quad &q)
{
cout << "\nCo-efficients are:\n";
cout << "a = " << q.a << "\n";
cout << "a = " << q.b << "\n";
cout << "a = " << q.c << "\n";
}
 

Smokey0066

Senior member
Oct 9, 1999
488
0
0
THANKS ALOT.

taken a HUGE load off my back.. i was stuck on this for a long time..

thanks again..

smokey
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: Smokey0066
THANKS ALOT.

taken a HUGE load off my back.. i was stuck on this for a long time..

thanks again..

smokey

No problem, but remember this is extremely simple and you have a long way to go in programming :)
 

Smokey0066

Senior member
Oct 9, 1999
488
0
0
hehe i'm only taking this because its a required class. i'm not very big into programing... just need to know enuff to get by.. hehe