This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++]another struct example with input  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]another struct example with input
« on: April 12, 2011, 05:29:44 AM »
well guys I feel like a discipled ninja who has been working hard cause his master ordered to work as in an old movie and all for this problem: http://evilzone.org/c-c/dynamic-structurevariables-help!/
however I didnt even solved it but It helped me to get more deep into struct functions so this problem I did it pretty fast so here we go:

problem:

7. William Wingate runs a pizza-analysis service. For each pizza, he needs to record the following
information:
• The name of the pizza company, which can consist of more than one word
• The diameter of the pizza
• The weight of the pizza
Devise a structure that can hold this information and write a program that uses a structure
variable of that type. The program should ask the user to enter each of the preceding
items of information, and then the program should display that information. Use cin
(or its methods) and cout.


solution:
Code: [Select]
#include <iostream>

struct pizzahut //this is the struct function well its like to declare the type of variable but //not the value is like to be more tidy
{
       char company[20];//here I just declare the type of variables
       float diameter;
       int weight;
};
int main()
{
    using namespace std;
    char company[20];//here dont confuse cause I just need to do this again
    float diameter;//cause is like this how struct works
    int weight;// look down and you will comprehend it  8)
   
    cout << "Enter the name of the company: ";
    cin.getline(company,20);//here I use this pretty cool function named cin.getline at the
    cout << endl;                 //next word we see the name of the variable which is going a take the value of the input and at the next we see the memory space that is going to use
now do you wonder why is it useful? just compile this program and enter more than one words then just replace cin.getline with just cin without memory store then try to use more than one word and you will see what happen
    cout << "Enter the radio of the pizza: ";
    cin >> diameter;
    cout << endl;
    diameter = diameter *  3,14159 * 2;
    cout << "Enter the weight of the pizza: ";
    cin >> weight;
    cout << endl;
   
    pizzahut * pizzahut_ptr = new pizzahut;//well Idk how to explain you this but here we
use this to work with the next line which Im gonna explain you below
   
    strcpy(pizzahut_ptr->company,company);//here we declare the variable with the
    pizzahut_ptr->diameter = diameter;//input values
    pizzahut_ptr->weight = weight;//seems complex but ptr means pointer I hope you to know about pointers cause I have no idea how to explain it  :(
   
    cout << (*pizzahut_ptr).company;//here goes the output compare how we work
    cout << endl;// when is about a char type
    cout << pizzahut_ptr->diameter;// and here a numerical type as int or float
    cout << endl;
    cout << pizzahut_ptr->weight;/here goes float output
    cout << endl;
   
    delete pizzahut_ptr;//here we delete the memory store if Im not wrong well I think that BoF are related to this topic well just google for it and you will find something useful maybe
    system("pause");
    return 0;
}


well I hope this was useful and I hope to ande and Satan dont get mad cause Im posting easly stuff about programming and all is disordered I may do a post which links everything in a future.
thanx for watching and do not doubt to make a question
« Last Edit: April 13, 2011, 05:51:21 AM by gh0st »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: another struct example with input
« Reply #1 on: April 12, 2011, 05:54:47 AM »
Well it's great you are sharing some code snippets but perhaps you should concentrate on posting more useful stuff or at least bigger examples (something at least above 100 lines of code). Even better would be if you came up with your own stuff instead of examples from a book.. But I know you are probably not there yet.
Satan911
Evilzone Network Administrator

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.