Thread: Odd output of an array of structures

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Odd output of an array of structures

    I verified the input successful and correct into the database array. However, I am not sure why the first two members of the structure are incorrect while the last one, the track, is correct;

    Code:
    struct music database[BUFSIZ];
    
    void populate(char *title, char *artist, int track, int index)
    {
    	database[index].title = title;
    	database[index].artist = artist;
    	database[index].track = track;
    
    }
    
    void dbDump(int index)
    {
    	using namespace std;
    
    	int i;
    	for (i = 0; i < index; i++)
    	{
    		cout << database[i].title << endl;
    		cout << database[i].artist << endl;
    		cout << database[i].track << endl;
    	}
    }
    
    void musicDb(int index)
    {
    	using namespace std;
    
    	clearBuffer();
    
    	cout << "Title: ";
    	char title[BUFSIZ];
    	cin.getline(title, sizeof(title));
    
    	cout << "Artist: ";
    	char artist[BUFSIZ];
    	cin.getline(artist, sizeof(artist));
    
    	cout << "Track: ";
    	int track;
    	cin >> track;
    
    	populate(title, artist, track, index);
    }
    
    int music()
    {
    	using namespace std;
    
    	bool run = true;
    	int index = 0;
    
    	while (run)
    	{
    		musicDb(index);
    
    		decide: cout << "Another entry (y or n)? ";
    		char decision;
    		cin >> decision;
    
    		if (decision - '0' >= 0 && decision - '0' <= 9)
    		{
    			cout << "Invalid entry!" << endl;
    			goto decide;
    		}
    
    		if (decision == 'n')
    		{
    			run = false;
    		}
    		else if (decision != 'y')
    		{
    			cout << "Invalid entry!" << endl;
    			goto decide;
    		}
    
    		index++;
    
    	}
    
    	dbDump(index);
    
    	return 0;
    }
    This program is for kicks and giggles as I learn C++. It is interesting to note that if only one entry is made, I can dump it fine. If more than one entry is made, it does not dump correctly;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Imanuel
    This program is for kicks and giggles as I learn C++.
    I'm kicking you and giggling as I look at your use of goto and a global variable

    Besides fixing these, you should show the definition of struct music.

    Quote Originally Posted by Imanuel
    It is interesting to note that if only one entry is made, I can dump it fine. If more than one entry is made, it does not dump correctly;
    It is interesting to note that in musicDb, title, artist and track are local variables, but in populate you assign pointers to them (or their contents) to a members of an object that will continue to exist even after they are destroyed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Take the time to sit down and learn C++, there are many things here that can easily be resolved if you bothered to learn the language. A good starting point is C++ Made Easy Tutorials. There is never a reason to use char arrays vice std::string, additionally you should be using a std::vector vice an array of structs here.

    A quick and dirty example:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    struct music{
    	std::string title;
    	std::string artist;
    };
    
    int main(void){
    
    	/*used here vice an array*/
    	std::vector<music>mymusic;
    	//used to print out database
    	std::vector<music>::iterator mymusic_it;
    	//used to store entries
    	music entry;
    	std::string answer;
    
    	//get input
    	do{
    		std::cout<<"Enter title: ";
    		std::cin >> entry.title;
    		std::cout<<"Enter artist: ";
    		std::cin >> entry.artist;
    		mymusic.push_back(entry); //add to "database"
    		std::cout<<"Continue(yes or no)";
    		std::cin>>answer;
    	}while(answer=="yes");
    
    	//print out "database"
    	for(mymusic_it=mymusic.begin();mymusic_it!=mymusic.end();++mymusic_it){
    		std::cout<<mymusic_it->artist<<" "<<mymusic_it->title<<std::endl;
    	}
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    178
    I admit to sloppy programming ...

    Quote Originally Posted by laserlight View Post
    I'm kicking you and giggling as I look at your use of goto
    Well I figured I would buck the system and use goto or snap my spurs into goto for bit then not use it.

    [
    Quote Originally Posted by laserlight View Post
    It is interesting to note that in musicDb, title, artist and track are local variables, but in populate you assign pointers to them (or their contents) to a members of an object that will continue to exist even after they are destroyed.
    Agreed, I was just busy typing away as I thought of it and this is prob why my output is not correct.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    178
    Ok I redid my sloppy code as follows:

    Code:
    struct music {
    	char title[BUFSIZ];
    	char artist[BUFSIZ];
    	int track;
    };
    
    void dbDump(int index, struct music database[])
    {
    	using namespace std;
    
    	int i;
    	for (i = 0; i < index; i++)
    	{
    		cout << database[i].title << endl;
    		cout << database[i].artist << endl;
    		cout << database[i].track << endl;
    	}
    }
    
    void musicDb(int index, struct music database[])
    {
    	using namespace std;
    
    	clearBuffer();
    
    	cout << "Title: ";
    	cin.getline(database[index].title, BUFSIZ);
    
    	cout << "Artist: ";
    	cin.getline(database[index].artist, BUFSIZ);
    
    	cout << "Track: ";
    	cin >> database[index].track;
    
    
    }
    
    int music()
    {
    	using namespace std;
    
    	bool run = true;
    	int index = 0;
    
    	struct music database[BUFSIZ];
    
    	while (run)
    	{
    
    		musicDb(index, database);
    
    		decide: cout << "Another entry (y or n)? ";
    		char decision;
    		cin >> decision;
    
    		if (decision - '0' >= 0 && decision - '0' <= 9)
    		{
    			cout << "Invalid entry!" << endl;
    			goto decide;
    		}
    
    		if (decision == 'n')
    		{
    			run = false;
    		}
    		else if (decision != 'y')
    		{
    			cout << "Invalid entry!" << endl;
    			goto decide;
    		}
    
    		index++;
    
    	}
    
    	dbDump(index, database);
    
    	return 0;
    }
    which works fine now although I am reluctant to free myself from the dangerous usage of goto although I am sure I will soon.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by AndrewHunter View Post
    Take the time to sit down and learn C++, there are many things here that can easily be resolved if you bothered to learn the language. A good starting point is C++ Made Easy Tutorials. There is never a reason to use char arrays vice std::string, additionally you should be using a std::vector vice an array of structs here.
    Understand that we all learn at different paces and the presentation of material is not consistent in some standard order across the board; i.e. when learning, topics in C++, everyone presents the language as they see fit for the student. I am not at the point of vectors or the string class. When I get there I am sure I will have new material to work with although thank you for an example that I may reference later when writing some code for practice containing these aspects of C++.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by AndrewHunter View Post
    Take the time to sit down and learn C++, there are many things here that can easily be resolved if you bothered to learn the language. A good starting point is C++ Made Easy Tutorials. There is never a reason to use char arrays vice std::string, additionally you should be using a std::vector vice an array of structs here.
    Understand that we all learn at different paces and the presentation of material is not consistent in some standard order across the board; i.e. when learning, topics in C++, everyone presents the language as they see fit for the student. I am not at the point of vectors or the string class. When I get there I am sure I will have new material to work with although thank you for an example that I may reference later when writing some code for practice containing these aspects of C++.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Lesson 3: Loops No need for goto.
    C++ Strings Drop the char arrays.
    EDIT:
    Quote Originally Posted by Imanuel View Post
    Understand that we all learn at different paces and the presentation of material is not consistent in some standard order across the board; i.e. when learning, topics in C++, everyone presents the language as they see fit for the student. I am not at the point of vectors or the string class. When I get there I am sure I will have new material to work with although thank you for an example that I may reference later when writing some code for practice containing these aspects of C++.
    And that is fine, I wasn't aware that this was a homework assignment. Just understand that there is a difference between programming in C++ and programming in C with classes.
    Last edited by AndrewHunter; 09-11-2011 at 07:25 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Imanuel View Post
    Understand that we all learn at different paces and the presentation of material is not consistent in some standard order across the board; i.e. when learning, topics in C++, everyone presents the language as they see fit for the student. I am not at the point of vectors or the string class. When I get there I am sure I will have new material to work with although thank you for an example that I may reference later when writing some code for practice containing these aspects of C++.
    It's not so much that you are learning things in a different order; it's that you are learning things that shouldn't ever happen. Technically, you can do 473 x 168 by sitting down and adding 168 to itself 473 times, but no one has ever actually learned that as a method of multiplication for three-digit numbers. Similarly, no one learning C++ has any excuse for coming across either a char array used in place of a string or the word "goto". (If you are coming from C, then you may feel somewhat comfortable with char arrays, but you still shouldn't be comfortable with goto even there; and keeping hold of C habits is just going to prevent you from learning C++, which is what has happened here.)

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by tabstop View Post
    It's not so much that you are learning things in a different order; it's that you are learning things that shouldn't ever happen. Technically, you can do 473 x 168 by sitting down and adding 168 to itself 473 times, but no one has ever actually learned that as a method of multiplication for three-digit numbers. Similarly, no one learning C++ has any excuse for coming across either a char array used in place of a string or the word "goto". (If you are coming from C, then you may feel somewhat comfortable with char arrays, but you still shouldn't be comfortable with goto even there; and keeping hold of C habits is just going to prevent you from learning C++, which is what has happened here.)
    Contrary to your statement here, none of the books, as I am going through them right now, are even teaching C++ except The C++ Programming Language:Special Edition and it has only taught on exception handling and namespaces thus far in my own reading. I learned C before learning C++ and the other resources I am using; C++ Primer Plus and Problem Solving with C++ have not taught C++ beyond <iostream> methods.

    The point I am making is that when teaching C++, many books teach C first then add-on the C++ topics of classes, inheritance, encapsulation, error handling, etc. I learned Java through my University so when I expected to see OOP topics right away they are all reserved for much later in the books I am reading whereas they were much further upfront in my Java book.

    I fully expect to code in C while learning C++ and the authors of the books I use intended it that way as C++ is an addition to C with OOP constructs. This is fine for me and I am happy with it.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Imanuel
    The point I am making is that when teaching C++, many books teach C first then add-on the C++ topics of classes, inheritance, encapsulation, error handling, etc.
    This "C then C++" methodology of teaching C++ has been criticised by Professor Bjarne Stroustrup, the designer and original implementer of C++, in his essay on Learning Standard C++ as a New Language (PDF). Of course, authors of beginners' books for C++ will continue to write as they deem fit. One of the few exceptions is Koenig and Moo's Accelerated C++, which I recommend to anyone with some programming background.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  3. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 04:51 AM
  4. Replies: 3
    Last Post: 02-19-2003, 08:34 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM