0

This is the program i'm trying to develop. It's not an assignment or anything. I'm just a newbie trying out new stuff. But I'm getting frustrated with this.

#include <iostream>
using namespace std;

//========== struct ==========

struct nodeType
{ string song;
  int rating;
  nodeType *link;
}; 

//------------------------------------------------------------------------------
//========== function ==========

nodeType* song_info ()
{
  nodeType *first, *newNode, *last, *p;
  string Song;
  int Rating;
  
  //========== input ==========
  cout << "To end the program enter -999" << endl;
  
  cout << "Enter song : ";
  getline (cin, Song);
  cout << endl;
  
  cout << "Enter rating : ";
  cin >> Rating;
  cout << endl;
  
 
  first = NULL;
  
  while (Song != "-999")
  { 
    newNode = new nodeType;
    newNode -> song = Song;    

    newNode -> rating = Rating;
    
    
    newNode -> link = NULL;
    
    if (first == NULL)
    { 
      first = newNode;
      last = newNode;
    }

    else
    
    {
      last -> link = newNode;
      last = newNode;
    }
    
  cout << "Enter song : ";
  getline (cin, Song);
  cout << endl;
  
  cout << "Enter rating : ";
  cin >> Rating;
  cout << endl;
  } 
  
  system ("cls");
  cout << endl;
  cout << "=========================================================="<< endl;

  //========== display ==========

  cout << endl;
  last = first;
  while (last != NULL)
  { 
   cout << "Song       : " << last -> song << endl << endl 
        << "Rating     : " << last -> rating << endl << endl;
   last = last -> link;
   cout << endl << endl;
   cout << "=========================================================="<< endl;
   cout << endl; 
  }
}

//------------------------------------------------------------------------------
//========== main ==========
int main ()
{
  song_info();
  
  system ("pause");
  return 0;
}

The first input was alright, the problem is the second round of the loop. Please help me with this.

Edited by Alexia Ooi: n/a

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by Salem
0

You have one massive function to do everything.

Break your list steps into
- initialise list
- append to list
- print list
- destroy list

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.