locked
What are structures and why are they useful? RRS feed

  • Question

  • I am trying to write this code using structures but I am getting nowhere...

    #include <iostream>
    
    using namespace std;
    
    
    struct starType
    {
    	string starName;
    };
    struct timeType
    {
    	int primeTime;
    	int hour;
    	int minute;
    	int day;
    	int month;
    	int year;
    	int star;
    	int planet;
    };
    struct priceType
    {
    	int primePrice;
    	int price;
    };
    struct astronomyType
    {	
    	starType star;
    	string stars;
    	string planet;
    };
    struct dayType
    {
    	timeType day;
    	int primeDay;
    };
    
    struct productType
    {
    	string product;
    };
    struct categoryType
    {
    
    	string category;
    
    	double computersAccessories;
    	double music;
    	double other;
    	double price;
    	double miscellaneous;
    	double cigarettes;
    	double books;
    	double clothing;
    	double rX;
    	double groceries;
    	double mail;
    };
    struct savingsType
    {
    	double savings;
    };
    struct webSiteType
    {
    	int webPrime;
    	string webLocation;
    };
    
    
    int main()
    {
    astronomyType newAstronomy;
    newAstronomy.star.starName = "Altair";
    }
    
    This book skips a lot of information... C++ Programming by Malik
    
    
    
    


    MGM

    Monday, October 8, 2012 5:16 AM

Answers

All replies

  • I am trying to write this code using structures

    Looks like it is written.

    >but I am getting nowhere...

    To where *exactly* do you want to get?

    There are abundant references available on the Web. You
    should get many hits using your favourite search engine.

    e.g. -

    C++ Programming/Structures
    http://en.wikibooks.org/wiki/C%2B%2B_Programming/Structures

    Using Structures in C++
    http://www.inversereality.org/files/structures.pdf

    Data Structures
    http://cplusplus.com/doc/tutorial/structures/

    Lesson 7: Structures in C
    http://www.cprogramming.com/tutorial/c/lesson7.html

    Introduction to Classes
    Composite Types
    http://www.functionx.com/cpp/Lesson20.htm

    - Wayne

    • Proposed as answer by Nag a Thursday, October 11, 2012 8:28 PM
    • Marked as answer by NK-7 Monday, October 15, 2012 9:58 PM
    Monday, October 8, 2012 6:58 AM
  • In this example what is #define?

    // array of structures
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    #define N_MOVIES 3
    
    struct movies_t {
      string title;
      int year;
    } films [N_MOVIES];
    
    void printmovie (movies_t movie);
    
    int main ()
    {
      string mystr;
      int n;
    
      for (n=0; n<N_MOVIES; n++)
      {
        cout << "Enter title: ";
        getline (cin,films[n].title);
        cout << "Enter year: ";
        getline (cin,mystr);
        stringstream(mystr) >> films[n].year;
      }
    
      cout << "\nYou have entered these movies:\n";
      for (n=0; n<N_MOVIES; n++)
        printmovie (films[n]);
      return 0;
    }
    
    void printmovie (movies_t movie)
    {
      cout << movie.title;
      cout << " (" << movie.year << ")\n";
    }


    MGM

    Monday, October 8, 2012 11:01 AM
  • You can use the #define directive to give a meaningful name to a constant in your program . In the Above sample you are telling that you want three object of struct movies_t and your for loop will run till the time no of moves is less then 3 which is 0 1 2. Alternative of #define is you can use const

    like

    Thanks


    Rupesh Shukla


    const int N_MOVIES = 3; // pick some book to read the difference between macro and constant
    • Edited by Pintu Shukla Monday, October 8, 2012 12:43 PM
    • Proposed as answer by Elegentin Xie Monday, October 15, 2012 10:54 AM
    Monday, October 8, 2012 12:40 PM
  • Now that I have the structure somewhat built I would like to pour data into these variables...

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    struct starType
    {
    	int starNumber;
    	char starName[20];
    };
    struct planetType
    {
    	int planetNumber;
    	char planetName[20];
    };
    struct timeType
    {
    	int primeTime;
    	int actualTime;
    	int hour;
    	int minute;
    	int day;
    	int month;
    	int year;
    	int star;
    	int planet;
    };
    struct priceType
    {
    	int primePrice;
    	int price;
    };
    struct dayType
    {
    	timeType day;
    	int primeDay;
    };
    
    struct productType
    {
    	string product;
    };
    struct categoryType
    {
    
    	string category;
    
    	double computersAccessories;
    	double music;
    	double other;
    	double price;
    	double miscellaneous;
    	double cigarettes;
    	double books;
    	double clothing;
    	double rX;
    	double groceries;
    	double mail;
    };
    struct savingsType
    {
    	double savings;
    };
    struct webSiteType
    {
    	int webPrime;
    	string webLocation;
    };
    
    int main()
    {
    	starType a2 = {2, "Sirius"};
    	starType a3 = {3, "Procyon"};
    	starType a5 = {5, "Pollux"};
    	starType a7 = {7, "Regulus"};
    	starType a11 = {11, "Spica"};
    	starType a13 = {13, "Arcturus"};
    	starType a17 = {17, "Antares"};
    	starType a19 = {19, "Vega"};
    	starType a23 = {23, "Altair"};
    	starType a29 = {29, "Deneb"};
    	starType a31 = {31, "Deneb"};
    	starType a37 = {37, "Fomalhaut"};
    	starType a41 = {41, "Aldebaren"};
    	starType a43 = {43, "Rigel"};
    	starType a47 = {47, "Capella"};
    	starType a53 = {53, "Betelgeuse"};
    
    	cout << a2.starNumber << '\t' << a2.starName << endl;
    	cout << a3.starNumber << '\t' << a3.starName << endl;
    	cout << a5.starNumber << '\t' << a5.starName << endl;
    	cout << a7.starNumber << '\t' << a7.starName << endl;
    	cout << a11.starNumber << '\t' << a11.starName << endl;
    	cout << a13.starNumber << '\t' << a13.starName << endl;
    	cout << a17.starNumber << '\t' << a17.starName << endl;
    	cout << a19.starNumber << '\t' << a19.starName << endl;
    	cout << a23.starNumber << '\t' << a23.starName << endl;
    	cout << a29.starNumber << '\t' << a29.starName << endl;
    	cout << a31.starNumber << '\t' << a31.starName << endl;
    	cout << a37.starNumber << '\t' << a37.starName << endl;
    	cout << a41.starNumber << '\t' << a41.starName << endl;
    	cout << a43.starNumber << '\t' << a43.starName << endl;
    	cout << a47.starNumber << '\t' << a47.starName << endl;
    	cout << a53.starNumber << '\t' << a53.starName << endl;
    
    	planetType p2 = {2, "Mercury"};
    	planetType p3 = {3, "Venus"};
    	planetType p5 = {5, "Mars"};
    	planetType p7 = {7, "Jupiter"};
    	planetType p11 = {11, "Saturn"};
    	planetType p13 = {13, "Uranus"};
    	planetType p17 = {17, "Neptune"};
    	planetType p19 = {19, "Pluto"};
    
    	cout << p2.planetNumber << '\t' << p2.planetName << endl;
    	cout << p3.planetNumber << '\t' << p3.planetName << endl;
    	cout << p5.planetNumber << '\t' << p5.planetName << endl;
    	cout << p7.planetNumber << '\t' << p7.planetName << endl;
    	cout << p11.planetNumber << '\t' << p11.planetName << endl;
    	cout << p13.planetNumber << '\t' << p13.planetName << endl;
    	cout << p17.planetNumber << '\t' << p17.planetName << endl;
    	cout << p19.planetNumber << '\t' << p19.planetName << endl;
    
    	timeType t2 = {2};
    	timeType t3 = {3};
    	timeType t5 = {5};
    	timeType t7 = {7};
    	timeType t11 = {11};
    	timeType t13 = {13};
    	timeType t17 = {17};
    	timeType t19 = {19};
    	timeType t23 = {23};
    	timeType t29 = {29};
    	timeType t31 = {31};
    	timeType t37 = {37};
    	timeType t41 = {41};
    	timeType t43 = {43};
    	timeType t47 = {47};
    	timeType t53 = {53};
    	timeType t59 = {59};
    	timeType t101 = {101};
    	timeType t103 = {103};
    	timeType t107 = {107};
    	timeType t109 = {109};
    	timeType t113 = {113};
    	timeType t127 = {127};
    	timeType t131 = {131};
    	timeType t137 = {137};
    	timeType t139 = {139};
    	timeType t149 = {149};
    	timeType t151 = {151};
    	timeType t157 = {157};
    	timeType t211 = {211};
    	timeType t223 = {223};
    	timeType t227 = {227};
    	timeType t229 = {229};
    	timeType t233 = {233};
    	timeType t239 = {239};
    	timeType t241 = {241};
    	timeType t251 = {251};
    	timeType t257 = {257};
    	timeType t307 = {307};
    	// 311 313 317 331 337 347 349 353 359 401 409 419 421 431 433 439 443 449 457 503 509 521 523 541 547 557 601 607 613 617 619 631 641 643 647 653 659 701 709 719 727 733 739 743 751 757 809 811 821 823 827 829 839 853 857 859 907 911 919 929 937 941 947 953 1009 1013 1019 1021 1031 1033 1039 1049 1051 1103 1109 1117 1123 1129 1151 1153 1201 1213 1217 1223 1229 1231 1237 1249 1259 1301 1303 1307 1319 1321 1327 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1511 1523 1531 1543 1549 1553 1559 1601 1607 1609 1613 1619 1621 1627 1637 1657 1709 1721 1723 1733 1741 1747 1753 1759 1801 1811 1823 1831 1847 1901 1907 1913 1931 1933 1949 1951 2003 2011 2017 2027 2029 2039 2053 2111 2113 2129 2131 2137 2141 2143 2153 2203 2207 2213 2221 2237 2239 2243 2251 2309 2311 2333 2339 2341 2347 2351 2357
    timeType a1;
    
    cin >> a1.actualTime;
    
    	if (a1.actualTime < t2.primeTime)
    	{
    		a1.actualTime = t2.primeTime;
    		cout << a1.actualTime << endl;
    	}
    	if (t3.primeTime < a1.actualTime < t5.primeTime)
    	{
    		a1.actualTime = t5.primeTime;
    		cout << a1.actualTime << endl;
    	}
    	if (t5.primeTime < a1.actualTime < t7.primeTime)
    	{
    		a1.actualTime = t7.primeTime;
    		cout << a1.actualTime <<endl;
    	}
    }

    except I would like to import the data instead of hard-coding it.


    MGM

    Monday, October 8, 2012 1:04 PM
  • Look back on your old example how you was doing with cin to get the data for structure same way you have to get data here too . cin function that's all.

    Thanks


    Rupesh Shukla

    Monday, October 8, 2012 1:41 PM
  • How about something like this?

    	const int KCOLS = 211;
    	ifstream data4;
    	int Time[KCOLS] = {0};
    
    int main()
    {
    
     data4.open("Time.dat");
      if(data4.fail())
      {
    	  cerr << "Error opening data file\n";
    	  exit(1);
      }
         for (int i = 0; i< KCOLS; ++i)
        {
    		timeType t[211];
    		data4 >> t[i].primeTime;
    		if(!data4) break;
                    {
    					cout << t[i].primeTime << '\t';
                    }
            
        }
      if(!data4.eof())
        {
        cout << "Read error4!\n";
        return -1;
        }
    data4.clear();
      data4.close();
    }


    MGM

    Monday, October 8, 2012 1:48 PM
  • I am confused now . From where this file handling comes in the picture now. See it's upto you from where ever you are reading data from console or from file . Use proper stream and store the data inside your structure . And when you are reading from file you don't need any hardcoded loop etc. Better to have a look  in some book for file handling in C++ .

    Thanks


    Rupesh Shukla

    Monday, October 8, 2012 1:53 PM
  • timeType a1;
    timeType t[211];
    
    cin >> a1.actualTime;
    	for (int i = 0; i<KCOLS; ++i)
    	{
    		if (a1.actualTime == t[i].primeTime)
    		{
    			break;
    		}
    		else
    		{
    			cout << a1.actualTime << endl;
    			a1.actualTime++;
    		}
    	}

    I would like that the program round everything up to the next prime number...

    so if the actualTime is 6 I would like that it be changed to 7

    if actual time is 8 I would like it to be 11.

    Whjat is wrong with my loop?


    MGM

    Monday, October 8, 2012 2:16 PM
  • There is nothing wrong with your for loop it will run from 0 to KCOLS check your logic for the prime number.

    Thanks


    Rupesh Shukla

    Monday, October 8, 2012 2:32 PM
  • I am afraid it is not working...


    MGM

    The array is not being stored outside of the scope of the first for loop...

    How would I solve that problem?

    • Edited by NK-7 Monday, October 8, 2012 3:32 PM
    Monday, October 8, 2012 3:04 PM
  • I am pretty sure about for loop . regarding prime no search on the net there are n no of thread which will tell you how to create a prime no. Put a break point inside your code and debug your application.

    Thanks


    Rupesh Shukla

    Monday, October 8, 2012 4:31 PM
  • Can I put this data extraction in a header file?

    MGM

    Monday, October 8, 2012 5:14 PM
  • Depends your code structure .If you are using class concept in that scenario you can have all decleration inside a header file and all definition inside your cpp file. But even if you want you can put extraction code in some .h file and simply include that file inside your project.

    Thanks


    Rupesh Shukla

    Monday, October 8, 2012 5:38 PM
  • #include "star.h"
    #include "planet.h"
    #include "category.h"
    #include "day.h"
    #include "price.h"
    #include "product.h"
    #include "savings.h"
    #include "time.h"
    #include "website.h"

    I have placed each structure in their own header file... is that wise?

    MGM

    Monday, October 8, 2012 6:35 PM
  • usually this is a good idea but as i mentioned when your class or structure having member function or some kind of Relationship between them.In that scenario each class has its own header file and cpp file  . But if structure just contain datatype then in that scenario you can have all the structure at a single place . Again it's depend on your programming style and how do you like it. If file size is too big then you can devide it in two or more then two files and thean add all the file inside your cpp.

    Thanks


    Rupesh Shukla

    Monday, October 8, 2012 6:39 PM
  • How would I combine multiple .cpp files?

    MGM

    Monday, October 8, 2012 6:43 PM
  • You really need to get a reference.  You cannot learn how to program in C++ by asking a question each time you come upon a different aspect of the language.
    • Proposed as answer by Elegentin Xie Monday, October 15, 2012 10:54 AM
    Monday, October 8, 2012 6:49 PM
  • How would I combine multiple .cpp files?

    MGM

    You Don't have to combine .cpp file you have to use .h file to access member class and function defined inside a .h file . Just start reading a good book on C++ and you will be learn things in a efficient manner.

    Thanks


    Rupesh Shukla

    • Proposed as answer by Elegentin Xie Monday, October 15, 2012 10:54 AM
    Monday, October 8, 2012 6:58 PM
  • I see I need some function prototypes...

    MGM

    Tuesday, October 9, 2012 6:09 PM