Okay, I have a slight problem here. My final project was pretty much open to anything. We just had to make a proposal and do it. So, I proposed a bank type program. I had it done and in the bag(or so I thought) until I was told to include linked lists somewhere in it. So, I decided to sorta keep track of the deposits in one linked list and keep track of withdrawals in another linked list. However, I keep getting errors that say "jump to case label - crosses initialization of Nodetype newNodePtr". I know it has something to do with me trying to use linked lists inside a switch function. I really do not want to change the format of my entire program. Is there any way around this? My code is below:
//**************************************************************************
//This program simulates the services offered by a bank. The user can make
//deposits, withdraw, check their balance.
//***************************************************************************
#include <iostream>
#include <string>
#include <cstddef> //to access NULL
using namespace std;
struct NodeType; //forward declaration
typedef NodeType* NodePtr;
struct NodeType
{
char transaction;
float amt;
float total;
NodePtr next;
};
void chooseService();
float balance=0.00;
NodePtr listPtr = NULL; //external pointer
int main()
{
string usernameFirst, usernameLast;
char answer;
cout<<"\n ***Welcome to the Bank of AA&J***\n\n\n";
cout<<"Please enter your name: ";
cin>>usernameFirst>>usernameLast;
cout<<"\n\nWelcome "<<usernameFirst<<" "<<usernameLast<<"!"<<endl;
cout<<"\n";
chooseService();
action:
cout<<"\nWould you like to perform another action? (y or n)";
cin>>answer;
if(answer=='n')
{
cout<<"\nThank you! Goodbye!\n\n";
}
if(answer=='y')
{
chooseService();
goto action;
}
system("pause");
return 0;
}
//***************************************************************************
void chooseService()
{
char service;
float add;
float subtract;
cout<<"\nPlease choose a service: "<<
"\n D-to deposit"<<
"\n W-to withdraw"<<
"\n C-to check balance"<<
"\n H-to see account history\n";
cin>>service;
switch(service)
{
case 'D' : cout<<"Enter amount to deposit: $";
cin>>add;
balance=balance+add;
NodePtr newNodePtr=new NodeType;
newNodePtr->amt=add;
newNodePtr->total=balance;
listPtr=newNodePtr;
break;
case 'W' : cout<<"Enter amount to withdrawal: $";
cin>>subtract;
balance=balance-subtract;
break;
case 'C' : cout<<"Your balance is: ";
cout<<"$"<<balance;
break;
case 'H' : NodePtr currNodePtr;
currNodePtr = listPtr;
while (currNodePtr != NULL)
{
cout<<"The transaction amount was: $"<< currNodePtr->amt<< endl;
cout<<"Balance: $"<<currNodePtr->total<<endl;
currNodePtr = currNodePtr -> next;
}
break;
default : cout<<"error()";
break;
}
}
//****************************************************************************
If you take out everything pointer/linked list related, it compiles perfectly. Just need ideas on how to do a linked list inside a switch.
Thanks for any help as usual!