| | |
Structure Help Please
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Solved Threads: 0
This is my program but that I have to put it into structure form but without using member functions but i dont see how I can make it run
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip.h>
struct hope()
{
int x;
char st_name[25];
char st_address[75];
char st_color[25];
float f_ccost;
float f_acost;
}
void main()
{
getdata();
processdata();
putdata();
}
void getdata()
{
cout << "\n\nPlease enter your name: ";
cin.getline(st_name,25);
cout << "\n\nPlease enter your address: ";
cin.getline(st_address,75);
cout << "\n\nPlease enter the amount you paid for your car: ";
cin >> f_acost;
cin.get();
cout << "\n\nPlease enter your car color: ";
cin.getline(st_color,25);
}
void processdata()
{
cout << "\n\nCalculated amount of your car is: ";
if(f_acost < 1200)
{
if(strcmp(st_color,"blue")==0)
{
f_ccost = f_acost + f_acost*10/100;
cout << f_ccost;
}
else
{
cout << f_acost;
}
}
else if(f_acost > 1000 && f_acost < 1500)
{
if(strcmp(st_color,"black")==0)
{
f_ccost = f_acost + f_acost*20/100;
cout << f_ccost;
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
else if(f_acost <= 2000)
{
if(strcmp(st_color,"white")==0)
{
f_ccost = f_acost - 500;
cout << f_ccost;
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
void putdata()
{
cout << "\nPerson Name: " << st_name;
cout << "\nAddress: " << st_address;
cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << "\nActual Cost of Car: $ " << f_acost;
cout << "\nCalculated Cost of Car: $ " << f_ccost;
cout << "\nColor of Car: " << st_color;
}
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip.h>
struct hope()
{
int x;
char st_name[25];
char st_address[75];
char st_color[25];
float f_ccost;
float f_acost;
}
void main()
{
getdata();
processdata();
putdata();
}
void getdata()
{
cout << "\n\nPlease enter your name: ";
cin.getline(st_name,25);
cout << "\n\nPlease enter your address: ";
cin.getline(st_address,75);
cout << "\n\nPlease enter the amount you paid for your car: ";
cin >> f_acost;
cin.get();
cout << "\n\nPlease enter your car color: ";
cin.getline(st_color,25);
}
void processdata()
{
cout << "\n\nCalculated amount of your car is: ";
if(f_acost < 1200)
{
if(strcmp(st_color,"blue")==0)
{
f_ccost = f_acost + f_acost*10/100;
cout << f_ccost;
}
else
{
cout << f_acost;
}
}
else if(f_acost > 1000 && f_acost < 1500)
{
if(strcmp(st_color,"black")==0)
{
f_ccost = f_acost + f_acost*20/100;
cout << f_ccost;
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
else if(f_acost <= 2000)
{
if(strcmp(st_color,"white")==0)
{
f_ccost = f_acost - 500;
cout << f_ccost;
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
else
{
f_ccost = f_acost;
cout << f_ccost;
}
}
void putdata()
{
cout << "\nPerson Name: " << st_name;
cout << "\nAddress: " << st_address;
cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << "\nActual Cost of Car: $ " << f_acost;
cout << "\nCalculated Cost of Car: $ " << f_ccost;
cout << "\nColor of Car: " << st_color;
}
Greetings hopeolicious,
A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
Structure Basics
The keyword struct introduces a sturcture declaration, which is a list of declarations enclosed in braces.
An optional name called a structure tag may follow the word struct. The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.
A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type.
A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or the shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, from the given declaration above: This defines a variable box_1 which is a structure of the type struct MyBox. A structure can be initialized by following its definition with a list of initializers, each a constant expression for the members: An automatic structure may also be intialized by assignment or by calling a function that returns a structure of the right type.
The structure member operator "." connects the structure name and the member name. To print the coordinates of the integer "left" of box_1 for instance would compute as the following: The syntax is simple, structure-name . member
Implementing
As continuing from above, you may see that your structure contains a few syntactial errors: The red-colored section(s) may cause an error, and the green-colored section(s) show what needs to be added. Before I move to member-calling, let me clarify a few things. Your struct called hope cannot be called on if your structure tag is the same. You will have to either name your structure differently while defining the variable of your struct to hope: Or just choose a different name while declaring the variable name declaration.
On to the member calls, when you call on something, say, f_acost, you cannot call it normally. You will have to take the neccessary steps to call on such a structure member, for example purposes: You will have to find all members from within your structure and change all to look like the above.
Here is an example of how to allow all of your functions to see and recognize your structure:
Conclusion
I hope this helps. If you have further questions, please feel free to ask.
- Stack Overflow
A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
Structure Basics
The keyword struct introduces a sturcture declaration, which is a list of declarations enclosed in braces.
struct MyBox { int left; int right; int top; int bottom; };
An optional name called a structure tag may follow the word struct. The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.
A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type.
A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or the shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, from the given declaration above:
struct MyBox box_1;struct MyBox box_1 = {0, 5, 0, 10};The structure member operator "." connects the structure name and the member name. To print the coordinates of the integer "left" of box_1 for instance would compute as the following:
C++ Syntax (Toggle Plain Text)
box_1.left = 0;
Implementing
As continuing from above, you may see that your structure contains a few syntactial errors:
struct hope() { ... };
struct Hope { ... }; [/b]Hope hope;
On to the member calls, when you call on something, say, f_acost, you cannot call it normally. You will have to take the neccessary steps to call on such a structure member, for example purposes:
C++ Syntax (Toggle Plain Text)
hope.f_acost = 0.0f;
Here is an example of how to allow all of your functions to see and recognize your structure:
struct Hope { float f_acost; }; Hope hope; int main() { hope.f_acost = 0.0f; return 0; }
Conclusion
I hope this helps. If you have further questions, please feel free to ask.
- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Similar Threads
- Discrete Math or Data structure (Computer Science)
- Can array works within Structure? (C)
- Question regarding correct table structure (HTML and CSS)
- Scandisc stops at "File Structure" (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Problem in tryAgain function. Please Help!
- Next Thread: cl.exe and Microsoft Visual C++ 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





