d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Hey Guys I Need Some C++ Help
12Next
Add Reply New Topic New Poll
Member
Posts: 10,884
Joined: Jul 7 2007
Gold: 316.00
Oct 26 2012 04:09pm
My program compiles correctly but when I choose one of the options nothing comes up, and I cannot figure out why. If anyone can please help me that would be awesome.... I know my while loop in the main is not completed yet.

I believe my problem is either in the loadlist or the print function.
Please help :(



here is my code



#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

//Struct
struct PublicOfficial
{
string name;
string sob;
int code;
int presorder;
int firstpterm;
int lastpterm;
int vpresorder;
int firstvpterm;
int lastvpterm;
PublicOfficial *nextofficial;
};

//Globals
ifstream infile;

//Prototypes
void LoadList ( PublicOfficial *&firstofficial );
void addOfficial ( PublicOfficial *&firstofficial, PublicOfficial *nextofficial );
void printall ( PublicOfficial *firstofficial, PublicOfficial code);
void printoneterm ( PublicOfficial *firstofficial );
int menu();

int main()
{
PublicOfficial *firstofficial = NULL;
PublicOfficial code;
int choice;

//Calling LoadList
LoadList ( firstofficial );

//Calling Menu
choice = menu();

while ( choice != 7 )
{
if ( choice == 1 )
{
printall ( firstofficial, code );
}
else if ( choice == 2 )
printall ( firstofficial, code );
else if ( choice == 3 )
printall ( firstofficial, code );
else if ( choice == 4 )
printoneterm ( firstofficial );
//Calling Menu
menu();
}
return 0;

} // End of Main

/*********************************************************************
*Loadlist function will accept the head pointer by reference. It will*
*also read in data on the Presidents and Vice Presidents of the US.It*
*will continue to read until the EOF marker. *
*********************************************************************/
void LoadList ( PublicOfficial *&firstofficial )
{
//Opening the file
infile.open( "officials.txt" );
if (!infile)
{
cout << "This file officials.txt does not exist." << endl;
exit(1);
}
PublicOfficial *newofficial;

while (!EOF)
{
newofficial = new PublicOfficial;
newofficial->nextofficial = NULL;
getline(infile, newofficial->name);
getline(infile, newofficial->sob);
infile >> newofficial->code;
if ( newofficial->code == 3 )
{
infile >> newofficial->presorder;
infile >> newofficial->firstpterm;
infile >> newofficial->lastpterm;
infile >> newofficial->vpresorder;
infile >> newofficial->firstvpterm;
infile >> newofficial->lastvpterm;
}
else if ( newofficial->code == 2 )
{
newofficial->presorder = 0;
newofficial->firstpterm = 0;
newofficial->lastpterm= 0;
infile >> newofficial->vpresorder;
infile >> newofficial->firstvpterm;
infile >> newofficial->lastvpterm;
}
else if (newofficial->code == 1)
{
infile >> newofficial->presorder;
infile >> newofficial->firstpterm;
infile >> newofficial->lastpterm;
newofficial->vpresorder = 0;
newofficial->firstvpterm = 0;
newofficial->lastvpterm = 0;
}

//Calling Add Official
addOfficial ( firstofficial, newofficial );
}
//Closing the file
infile.close();

}

/*********************************************************************
*Passes the head points by reference and the newofficial to be added *
*This function will add this node to the end of the linked list, this*
*function will be called from within the while loop for each new *
*official. *
*********************************************************************/
void addOfficial ( PublicOfficial *&firstofficial, PublicOfficial *newofficial )
{
PublicOfficial *Tptr;
Tptr = firstofficial;
firstofficial = newofficial;
newofficial->nextofficial = Tptr;
}

/*********************************************************************
*A menu that will return a valid menu choice based on criteria that *
*the user wishes to see. *
*********************************************************************/
int menu ()
{
int choice;

cout << endl;
cout << "========================================" << endl;
cout << " 1. List Presidents " << endl;
cout << " 2. List Vice Presidents " << endl;
cout << " 3. List Officials that held both " << endl;
cout << " 4. List One Term Presidents " << endl;
cout << " 5. List Presidents by the century " << endl;
cout << " 6. List Presidents by birth place " << endl;
cout << " 7. Quit " << endl;
cout << "========================================" << endl;
cout << "Please enter your selection." << endl;
cin >> choice;
//Checking whether choice is vaild
while ( choice < 1 || choice > 7 )
{
cout << "This is an incorrect choice, please choose another: ";
cin >> choice;
}
return choice;
}

/*********************************************************************
*A function that will pass the header and the code which is 1 and *
*print out all the presidents *
*********************************************************************/
void printall ( PublicOfficial *firstofficial, PublicOfficial code)
{
PublicOfficial *Tptr = firstofficial;

while (Tptr)
{
cout << "Name:" << setw(30) << Tptr->name << endl;
cout << "State:" << setw(30) << Tptr->sob << endl;
if (Tptr->code == 1)
{
cout << "President #:" << setw(30) << Tptr->presorder << endl;
cout << "Presidential Term:" << setw(30) << Tptr->firstpterm
<< " - " << Tptr->lastpterm << endl;
cout << "Vice President #:" << setw(30) << " N/A " << endl;
cout << "Vice Presidential Term:" << setw(30) << " N/A " << endl;
}
else if (Tptr->code == 2 )
{
cout << "President #:" << setw(30) << "N/A" << endl;
cout << "Presidential Term:" << setw(30) << "N/A" << endl;
cout << "Vice President #:" << setw(30) << Tptr->vpresorder << endl;
cout << "Vice Presidential Term:" << setw(30) << Tptr->firstvpterm
<< " - " << Tptr->lastvpterm << endl;
}
else if ( Tptr->code == 3 )
{
cout << "President #:" << setw(30) << Tptr->presorder << endl;
cout << "Presidential Term:" << setw(30) << Tptr->firstpterm
<< " - " << Tptr->lastpterm << endl;
cout << "Vice President #:" << setw(30) << Tptr->vpresorder << endl;
cout << "Vice Presidential Term:" << setw(30) << Tptr->firstvpterm
<< " - " << Tptr->lastvpterm << endl;
}
else
cout << "Error" << endl;
Tptr = Tptr->nextofficial;
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2012 04:11pm
Learn to debug. it's an experience every beginner needs.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 26 2012 04:27pm
cout << "========================================" << endl;
cout << " 1. List Presidents " << endl;
cout << " 2. List Vice Presidents " << endl;
cout << " 3. List Officials that held both " << endl;
cout << " 4. List One Term Presidents " << endl;
cout << " 5. List Presidents by the century " << endl;
cout << " 6. List Presidents by birth place " << endl;
cout << " 7. Quit " << endl;
cout << "========================================" << endl;
cout << "Please enter your selection." << endl;


so are you seeing all this when you run the program?

you said you are able to choose an option but nothing happens.
Member
Posts: 10,884
Joined: Jul 7 2007
Gold: 316.00
Oct 26 2012 04:27pm
Yes I do See all of that when the program runs and when I choose 1 and press enter, nothing happens
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Oct 26 2012 04:32pm
Quote (SlayingWhileInt0xicated @ Oct 26 2012 05:27pm)
Yes I do See all of that when the program runs and when I choose 1 and press enter, nothing happens


well I don't have time right now unfortunately but like you said I think the problem is where you think it lies (either the load or print), since you are using pointers you might want to draw a picture (helps a bunch) and see exactly what is going on.

Member
Posts: 10,884
Joined: Jul 7 2007
Gold: 316.00
Oct 26 2012 04:51pm
Ok I've messed around and now when I press 1 and enter

I get

Name:
State:
Error
Member
Posts: 10,884
Joined: Jul 7 2007
Gold: 316.00
Oct 26 2012 05:32pm
Will anyone take 25 FG to help me out!?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 26 2012 07:12pm
Quote (Eep @ Oct 26 2012 06:32pm)
well I don't have time right now unfortunately but like you said I think the problem is where you think it lies (either the load or print), since you are using pointers you might want to draw a picture (helps a bunch) and see exactly what is going on.


edit: sike

i say learn how to use a debugger and step through your code. you dont provide enough details about where its erroring what the error is what should happen what is happening or even data to test the app with.

This post was edited by AbDuCt on Oct 26 2012 07:16pm
Member
Posts: 10,884
Joined: Jul 7 2007
Gold: 316.00
Oct 26 2012 07:20pm
Quote (AbDuCt @ Oct 26 2012 08:12pm)
edit: sike

i say learn how to use a debugger and step through your code. you dont provide enough details about where its erroring what the error is what should happen what is happening or even data to test the app with.


The debugger isn't telling me anything.

I compile the program, there are no errors.
I run the program and the menu shows up,
I enter 1 and then all it does it show the menu again when it should call the printall function.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Oct 26 2012 07:27pm
Quote (SlayingWhileInt0xicated @ Oct 26 2012 08:20pm)
The debugger isn't telling me anything.

I compile the program, there are no errors.
I run the program and the menu shows up,
I enter 1 and then all it does it show the menu again when it should call the printall function.


How does the debugger not tell you anything?
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll