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;
}

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Stack Overflow
0

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.

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;

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:

struct MyBox box_1 = {0, 5, 0, 10};

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:

box_1.left = 0;

The syntax is simple, structure-name . member

Implementing
As continuing from above, you may see that your structure contains a few syntactial errors:

struct hope() {
...
};

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:

struct Hope {
[b]...[/b]
};

[/b]Hope 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:

hope.f_acost = 0.0f;

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:

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.