Get help on this challenge

Debugging4


Please read this explanation before attempting debugging challenges.


I put error checking and everything into my C++ code. Why does it break when I input something other than "list1" or "list2"?

//Made by COM, my thanks to Uber0n
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

typedef void (*ListInput)(string&);
void list1(string&);
void list2(string&);
int intInp();

int main()
{
 string temp;
 int amount;
 ListInput insert;
 bool notInt=true;
 cout<<"What list do you want to add to, list1 or list2 (list1/list2): ";
 cin>>temp;
 if(temp=="list1")
 {
  insert=&list1;
 }
 else if(temp=="list2")
 {
  insert=&list2;
 }
 cout<<"\nEnter the amount of lines you wish to insert: ";
 amount=intInp();
 for(int i=0; i<amount; i++)
 {
  cout<<"Enter line: ";
  getline(cin, temp);
  insert(temp);
 }
 return 0;
}

void list1(string& list1Inp)
{
 ofstream output;
 output.open("list1.txt", ios::app);
 output<<list1Inp<<endl;
 output.close();
 output.clear();
}

void list2(string& list2Inp)
{
 ofstream output;
 output.open("list2.txt", ios::app);
 output<<list2Inp<<endl;
 output.close();
 output.clear();
}

int intInp()
{
 int temp;
 static string intError;
 while(!(cin>>temp))
 {
  cin.clear();
  cin>>intError;
  cout<<"Bad value was input, try again: ";
 }
 cin.get();
 return temp;
}

Downloadable source code can be found here.


You must be logged in to play.



Challenge by COM.