This is supposed to be a code for a video game store

#include<iostream>
#include<fstream>
using namespace std;
ifstream infile;

struct VideoGame
{
       string Name;
       string Genre;
       string Publisher;
       string Developer;
       string Platform;       
       string Price;
       int count;
       VideoGame *link;
};
  void CreateList(VideoGame*& first, VideoGame*& last);
  void CheckOut(VideoGame*& first, string title);
  bool CheckTitle(VideoGame*& first, string title);
  void CheckIn(VideoGame*& first, string title);
  void InStock (VideoGame*& first, string title);
  void PrintTitle(VideoGame*& first);
  void PrintList( VideoGame*& first);
  void ShowMenu();
  int main()
  {
   
   
   if(!infile)
   {
   cout<<"no file "<<endl;
}
   string title;   
   int selection;   
  VideoGame *first, *last;
  CreateList(first,last);

 do
  {
    ShowMenu();
    cin>>selection;
    switch(selection)
    {
    case 1:
         cout<<"You have chosen to search if a video game title exist \nEnter the Video Game Title"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title))
         cout<<title<<" is currently in stock"<<endl;
         else
         {
          cout<<title<<" is not available in this store \n Please check your spelling"<<endl;
          }
          cout<<endl;
         break;
    case 2:
         cout<<"You are checking out a video game \n Enter the title of the video game"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title))
         {
         CheckOut(first,title);
         }
         else
         {
         cout<<title<<" is not in stock \n Please check your spelling"<<endl;
          }   
         break;
    case 3:
         cout<<"You are checking in a video game \n Enter the title of the video game"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title))
         {
         CheckIn(first,title);
         }
         else
         {
          cout<<title<<" is not available in this store \n Please check your spelling"<<endl;
          }
         
         break;
    case 4:
         cout<<"You are checking if a video game title is in stock \n Enter the title of the video game"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title))
         {
          InStock(first,title);
         }
         else
         {
          cout<<title<<" is not available in this store \n Please check your spelling"<<endl;
          }
         break;
    case 5:
         cout<<" These are the titles currently in our stores"<<endl;
         PrintTitle(first);
         break;
    case 6:
         cout<<" This is the Inventory"<<endl;
         PrintList(first);
         break;
    case 9:
         cout<<"Thank you, Have a Nice Day :)"<<endl;         
    default:
            cout<<"Invalid Selection"<<endl;
            }
}
while(selection!=9);

system("pause");
return 0;

}

void CreateList(VideoGame*& first, VideoGame*& last)                        
{
     infile.open("Project6.txt");
    first=NULL;
    last=NULL;
    VideoGame *newnode;                       
       string Name1;
       string Genre1;
       string Publisher1;
       string Developer1;
       string Platform1;       
       string Price1;
       int count1;
    while(infile)
    {
    newnode = new VideoGame;
     getline(infile,Name1);
     getline(infile,Genre1);
     getline(infile,Developer1);
     getline(infile,Publisher1);
     getline(infile,Platform1);
     getline(infile,Price1);
     infile>>count1;
     newnode->Name=Name1;
     newnode->Genre=Genre1;
     newnode->Publisher=Publisher1;
     newnode->Developer=Developer1;
     newnode->Platform=Platform1;
     newnode->Price=Price1;
     newnode->count=count1;
     newnode->link=NULL;
     if(first==NULL)
     {
     first=newnode;
     last=newnode;
     }
     else
         {
         last->link=newnode;
         last=newnode;
         }
     }
}
     
void ShowMenu()
{
     cout<<"----Menu----"<<endl;
     cout<<"\"1\" - to check if a title is in store"<<endl;
     cout<<"\"2\" - to check out a video game"<<endl;
     cout<<"\"3\" - to check in a video game"<<endl;
     cout<<"\"4\" - to see if a title is in stock"<<endl;
     cout<<"\"5\" - to print the video game titles"<<endl;
     cout<<"\"6\" - to print the inventory"<<endl;
     
}

bool CheckTitle(VideoGame*& first, string title)
{
     
    VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
      if(current->Name==title)
         return true;
      else
          return false;
      current=current->link;
      }
}

void CheckOut(VideoGame*& first, string title)
{
    VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
        if(current->Name==title)
        {                
       if(current->count>0)
       {
         current->count--;
         cout<<"Enjoy your "<<title<<endl;
         }
      else
          cout<<title<<" is out of stock"<<endl;
      }
      current=current->link;
      }
      cout<<endl;
      
}

void CheckIn (VideoGame*& first, string title)
{
   VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
        if(current->Name==title)
        {                
       if(current->count>0)
       {
         current->count++;
         cout<<"Thank you for trading in your "<<title<<endl;
         }
      }
      current=current->link;
      }
      cout<<endl;
         
}   

void InStock (VideoGame*& first, string title)
{
     VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
        if(current->Name==title)
        {                
       if(current->count>0)
         cout<<title<<" is in stock"<<endl;
         else
         cout<<title<<" is not in stock"<<endl;
      }
      current=current->link;
      }
      cout<<endl;
}

void PrintTitle(VideoGame*& first)
{
     VideoGame *current;
        current = first; 
       
        while (current != NULL)
        {
          cout << current->Name<<endl;
          current = current->link;
        }
        cout<<endl;

}
     
void PrintList( VideoGame*& first)     
{

        VideoGame *current;
        current = first; 
       
        while (current != NULL)
        {
          cout <<"Name :"<<current->Name <<"\n" <<"Genre(s) :"<< current->Genre
          <<"\n"  <<"Publisher(s) :"<<current->Publisher
          <<"\n"<<"Developer(s) :"<<current->Developer<<"\n"<<"Platform(s) :"
          <<current->Platform <<"\n"<<"Price :"<<current->Price<<"\n"
          <<"In Stock :"<<current->count<<endl;
          current = current->link;
        }
        cout<<endl;
}

This is my infile
Halo: Reach
First Person Shooter
Bungie
Microsoft Game Studios
Xbox 360
$ 59.99
7

BlazBlue: Calamity Trigger
Fighting
Arc System Works
Arc System Works / Aksys Games / pQube & Zen United
Arcade / PlayStation 3, PlayStation Portable, Xbox 360 / Games for Windows
$ 19.99
2

Resident Evil 5
Third-person shooter / survival horror
Capcom
Capcom
Xbox 360 / PlayStation 3 / Microsoft Windows
$ 29.99
0

Grand Theft Auto IV
Free-Roaming Sandbox / Action-Adventure Third Person Shooter
Rockstar North / Rockstar Toronto
Rockstar Games, Take-Two Interactive
$ 19.98
5


something is wrong with create list

Dani AI

Generated

Good that you tracked down the CreateList problem, , and that flagged the CheckTitle early-return bug. Below are concise, practical fixes and diagnostics that stop the program from only ever matching the first record and make the file/input handling more robust.

A correct search should only return false after the whole list is scanned. Do not return inside the loop except when you find the match. Example (minimal, different layout from the thread):

bool CheckTitle(VideoGame* first, const std::string& title) {
    for (VideoGame* cur = first; cur != nullptr; cur = cur->link)
        if (cur->Name == title) return true;
    return false;
}

File parsing and newline handling are other frequent causes of “only first item works.” Prefer reading every field with getline, convert the count from a small string, and strip a trailing CR if the file uses Windows line endings. Also check the file opened successfully right after open().

while (std::getline(infile, Name1)) {
    if (!std::getline(infile, Genre1)) break;
    // read other lines...
    std::string countLine;
    if (!std::getline(infile, countLine)) break;
    int count1 = std::stoi(countLine);
    if (!Name1.empty() && Name1.back() == '\r') Name1.pop_back();
    // create node and link it...
}

Mixing cin >> and getline causes leftover newlines to be read as empty titles. Fix that with cin.ignore() (or read the menu choice with getline and stoi). Quick debug tips: after building the list, print each title wrapped in delimiters (like |title|) to reveal stray whitespace or CR characters; use case-insensitive comparisons or trim input when matching user-entered titles. These small fixes will make the menu behave correctly for every entry.

Recommended Answers

All 3 Replies

ok i figured out the create list but now every menu option only work for Halo: Reach

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
ifstream infile;

struct VideoGame
{
       string Name;
       string Genre;
       string Publisher;
       string Developer;
       string Platform;       
       string Price;
       int count;
       VideoGame *link;
};
  void CreateList(VideoGame*& first, VideoGame*& last);
  void CheckOut(VideoGame*& first, string title);
  bool CheckTitle(VideoGame*& first, string title);
  void CheckIn(VideoGame*& first, string title);
  void InStock (VideoGame*& first, string title);
  void PrintTitle(VideoGame*& first);
  void PrintList( VideoGame*& first);
  void ShowMenu();
  int main()
  {
   
   char ch;
   string title;   
   int selection;   
  VideoGame *first, *last;
  CreateList(first,last);

 do
  {
    ShowMenu();
    cin>>selection;
    cin.get(ch);
    switch(selection)
    {
    case 1:
         cout<<endl;
         cout<<"You have chosen to search if a video game title exist \nEnter the Video Game Title"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title)==true)
         cout<<title<<" is currently in stock"<<endl;
         else
         {
          cout<<title<<" is not available in this store \nPlease check your spelling"<<endl;
          }
          cout<<endl;
         break;
    case 2:
         cout<<endl;
         cout<<"You are checking out a video game \nEnter the title of the video game"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title)==true)
         {
         CheckOut(first,title);
         }
         else
         {
         cout<<title<<" is not in stock \nPlease check your spelling"<<endl;
          }   
         break;
    case 3:
         cout<<endl;
         cout<<"You are checking in a video game \nEnter the title of the video game"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title)==true)
         {
         CheckIn(first,title);
         }
         else
         {
          cout<<title<<" is not available in this store \nPlease check your spelling"<<endl;
          }
         
         break;
    case 4:
         cout<<endl;
         cout<<"You are checking if a video game title is in stock \nEnter the title of the video game"<<endl;
         getline(cin,title);
         if(CheckTitle(first,title)==true)
         {
          InStock(first,title);
         }
         else
         {
          cout<<title<<" is not available in this store \n Please check your spelling"<<endl;
          }
         break;
    case 5:
         cout<<endl;
         cout<<" These are the titles currently in our stores"<<endl;
         PrintTitle(first);
         break;
    case 6:
         cout<<endl;
         cout<<" This is the Inventory"<<endl;
         PrintList(first);
         break;
    case 9:
         cout<<endl;
         cout<<"Thank you, Have a Nice Day :)"<<endl;         
    default:
            cout<<"Invalid Selection"<<endl;
            }
}
while(selection!=9);

system("pause");
return 0;

}

void ShowMenu()
{
     cout<<"----Menu----"<<endl;
     cout<<"\"1\" - to check if a title is in store"<<endl;
     cout<<"\"2\" - to check out a video game"<<endl;
     cout<<"\"3\" - to check in a video game"<<endl;
     cout<<"\"4\" - to see if a title is in stock"<<endl;
     cout<<"\"5\" - to print the video game titles"<<endl;
     cout<<"\"6\" - to print the inventory"<<endl;
     
}

void CreateList(VideoGame*& first, VideoGame*& last)                        
{
     char ch;     
     infile.open("Project6.txt");
    first=NULL;
    last=NULL;
    VideoGame *newnode;                       
       string Name1;
       string Genre1;
       string Publisher1;
       string Developer1;
       string Platform1;       
       string Price1;
       int count1;
    while(infile)
    {
    newnode = new VideoGame;
     getline(infile,Name1);
     getline(infile,Genre1);
     getline(infile,Developer1);
     getline(infile,Publisher1);
     getline(infile,Platform1);
     getline(infile,Price1);
     infile>>count1;
     cout<<count1;
     newnode->Name=Name1;
     newnode->Genre=Genre1;
     newnode->Publisher=Publisher1;
     newnode->Developer=Developer1;
     newnode->Platform=Platform1;
     newnode->Price=Price1;
     newnode->count=count1;
     newnode->link=NULL;
     infile.get(ch);
     infile.get(ch);
     if(first==NULL)
     {
     first=newnode;
     last=newnode;
     }
     else
         {
         last->link=newnode;
         last=newnode;
         }
     }
}

bool CheckTitle(VideoGame*& first, string title)
{
     
    VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
      if(current->Name==title)
      {
         return true;
         }
      else
      {
          return false;
          }
      current=current->link;
      }
}

void CheckOut(VideoGame*& first, string title)
{
    VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
        if(current->Name==title)
        {                
       if(current->count>0)
       {
         current->count--;
         cout<<"Enjoy your "<<title<<endl;
         }
      else
          cout<<title<<" is out of stock"<<endl;
      }
      current=current->link;
      }
      cout<<endl;
      
}

void CheckIn (VideoGame*& first, string title)
{
     int counter;
   VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
        if(current->Name==title)
        {                
       counter=current->count;              
       if(counter>0)
       {
         counter++;
         current->count=counter ;        
         cout<<"Thank you for trading in your "<<title<<endl;
         }
      }
      current=current->link;
      }
      cout<<endl;
         
}   

void InStock (VideoGame*& first, string title)
{    
     int counter;
     VideoGame *current;
    current=first;
    
    while(current!=NULL)
    {
        if(current->Name==title)
        { 
          counter=current->count;              
       if(counter>0)
         cout<<title<<" is in stock"<<endl;
         else
         cout<<title<<" is not in stock"<<endl;
      }
      current=current->link;
      }
      cout<<endl;
}

void PrintTitle(VideoGame*& first)
{
     VideoGame *current;
        current = first; 
       
        while (current != NULL)
        {
          cout << current->Name<<endl;
          current = current->link;
        }
        cout<<endl;

}
     
void PrintList( VideoGame*& first)     
{

        VideoGame *current;
        current = first; 
       
        while (current != NULL)
        {
          cout <<"Name :"<<current->Name <<"\n" <<"Genre(s) :"<< current->Genre
          <<"\n"  <<"Publisher(s) :"<<current->Publisher
          <<"\n"<<"Developer(s) :"<<current->Developer<<"\n"<<"Platform(s) :"
          <<current->Platform <<"\n"<<"Price :"<<current->Price<<"\n"
          <<"In Stock :"<<current->count<<endl;
          current = current->link;
          cout<<endl;
        }
        cout<<endl;
}

This is my new infile;
you have to copy and paste the exact spaces starting from below this line
Halo: Reach
First Person Shooter
Bungie
Microsoft Game Studios
Xbox 360
$ 59.99
7

BlazBlue: Calamity Trigger
Fighting
Arc System Works
Arc System Works / Aksys Games / pQube & Zen United
Arcade / PlayStation 3, PlayStation Portable, Xbox 360 / Games for Windows
$ 19.99
2

Resident Evil 5
Third-person shooter / survival horror
Capcom
Capcom
Xbox 360 / PlayStation 3 / Microsoft Windows
$ 29.99
0

Grand Theft Auto IV
Free-Roaming Sandbox / Action-Adventure Third Person Shooter
Rockstar North / Rockstar Toronto
Rockstar Games, Take-Two Interactive
Xbox 360 / Playstation 3 / Microsoft Windows
$ 19.98
5

One of your problems is the error in CheckTitle:

bool CheckTitle(VideoGame*& first, string title)
{  
  VideoGame *current;
  current=first;
  while(current!=NULL)
    {
      if(current->Name==title)
	{
	  return true;                
	}
      else
	{
          return false;
	}
      // You can't get to here :
      current=current->link;
    }
  // If you get here what do you return ???
}

I have added a couple of comments, simply put you are exiting false if the first title is not good. The else { } can be deleted.

In addition, looking for a game title by exact name seems a bit harsh, how about if you look for the length of the input string, e.g. Halo matches. You could be slight more sophisticated and return a tri-state value from check title, unique/multi/none.

p.s. Did you know that if you put two lines like this

std::string A="a long line split in two"
         " with some extra";

// String B is the same as string A.
std::string B="a long line split in two with some extra";

That may help you readability in a couple of places.

thanks but i figured out the problem
and you were right about the bool function

Thank you though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.