CHAR* STACK/QUEUE PROBLEM
Mar 8, 2013 at 5:39pm UTC
In my code, I cannot seem to push other string values other than the last value I have input. I know it's just a problem with storing string/char* in my stack/queue, because I have tried changing it to variable type int and it worked.
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <iostream>
#include <queue>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cctype>
using namespace std;
#define SONGS 7
int main()
{
queue<char *> n;
char downloads[SONGS][20]={"Song1" ,"Song2" ,"Song3" ,"Song4" ,"Song5" ,"Song6" ,"Song7" };
int ctr=-1;
char var[20];
char choice;
do {
cout<<"Enter a song: \n" ;
while (!(cin>>var))
{
cin.clear();
cin.ignore(80,'\n' );
}
n.push(var);
cout<<"Do you want to enter another song? (Y/N) : " ;
cin>>choice;
system("cls" );
}while (choice=='Y' || choice=='y' );
cout<<"Display input(s)" <<endl;
while (!n.empty())
{
for (int i=0;i<SONGS;i++)
{
if (!strcmp(n.front(),downloads[i]))
{
cout<<n.front()<<" is ready for download.\n" ;
}
else if (i==SONGS-1)
cout<<"Sorry " <<n.front()<<" doesn't exist in our song's download list.\n" ;
}
cout<<"\n\nDequeueing.. " <<n.front();
n.pop();
ctr++;
cout<<endl;
if (n.size()==0)
cout<<"\nQUEUE is now empty!" ;
}
return 0;
}
Mar 8, 2013 at 10:23pm UTC
A pointer holds a memory address.
You are pushing the same memory address into your queue while reusing the memory that address points to. Obviously, then, when you dereference the address, the original contents of that memory has changed because you've changed it through reuse.
If your intention is to store strings you have a couple options and none of them require that you repeatedly insert the same address into your queue. The simplest solution would be to use std::string to store the strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <limits>
int main()
{
std::queue<std::string> songs ;
std::string downloads[] = { "Song1" , "Song2" , "Song3" , "Song4" , "Song5" , "Song6" , "Song7" };
const unsigned nDownloads = sizeof (downloads) / sizeof (downloads[0]) ;
char choice = 'Y' ;
while ( choice == 'Y' || choice == 'y' )
{
std::cout << "Enter a song: \n" ;
std::string song ;
std::getline( std::cin, song ) ;
songs.push(song);
std::cout << "Do you want to enter another song? (Y/N): " ;
std::cin >> choice ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ) ;
}
std::cout << "Display input(s)\n" ;
while ( !songs.empty() )
{
std::string * const begin = downloads, *const end = downloads + nDownloads ;
std::string* found = std::find(begin, end, songs.front()) ;
if ( found != end )
std::cout << '"' << songs.front() << "\" is ready for download.\n" ;
else
std::cout << '"' << songs.front() << "\" is not in the download list.\n" ;
songs.pop() ;
}
}
Last edited on Mar 8, 2013 at 10:24pm UTC
Topic archived. No new replies allowed.