#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Need Help With Stopping Program From Exiting Execution


    Hello everyone,

    So I am working on a project in C++ to combine all the techniques I learned from TheNewBoston's YouTube page into a single program, and am running into some slight difficulties.

    The code itself for each goal section is fine on its own, but if I run the sections together, it will stop at various parts due to different things, like the break command from the switch statement example, or the .close command for the ofstream example, etc.

    Is there anyway I can get a continuous flow from one section to the next without it exiting the program completely?

    This is my code thus far, thanks for reading! :
    Code:
    #include <iostream>
    
    #include <fstream>
    // This allows me to use the ofstream function later to write on text files
    
    #include <cstdlib>
    // This includes a shut down function I'll later use
    
    #include <ctime>
    // This will be used to generate random numbers using the computer's internal clock, very
    // cool
    
    using namespace std;
    
    // Project Goals:
    
    // Goal 1: Create a C++ program that utilizes arrays in calculations
    
    // Status: Complete
    
    // Goal 2: Create a program that utilizes the address operator, which displays the memory address
    // for a particular variable
    
    // Status: Complete
    
    // Goal 3: Create a program that utilizes the pointer
    
    // Status: Complete
    
    // Goal 4: Create a program that utilizes the switch statement 
    
    // Status: Works, but exits the program after the user inputs a number for the legs,
    // Obviously, this is due to the break command
    
    // Goal 5: Create a program that writes on files
    
    // Status: Works on its own, but totally just exits the program again
    
    // Goal 6: Create a program that reads from a file
    
    // Status: Works on its own, but exits the program, probably due to the .close statement
    
    // Goal 7: Create a program that uses a random number generator
    
    // Create a program that ustilizes function overloading
    
    // Create a program that utilizes the idea of recursion 
    
    // Create a program that uses Arrays Into Functions 
    
    int main(){
    
    // Goal 1:
    
    	int list_of_numbers[7] = {1, 2 ,3 ,4 ,5, 6, 7};
    
    	int total = 0;
    
    	for(int i = 0; i < 7; i++)
    	{
    
    		total += list_of_numbers[i];
    
    		cout << total << endl;
    
    	}
    
    	cout << endl;
    
    // Goal 2:
    
    	int apples = 4;
    	
    	cout << "This the number of apples I have in my fridge: " << apples << endl;
    
    	cout << "This is the memory address for the apples variable: " << &apples << endl;
    
    // Goal 3:
    
    	int digits_on_one_hand = 5;
    
    	int *pointer;
    
    	pointer = &digits_on_one_hand;
    
    	cout << "Here is the value of the digits_on_one_hand variable: " << digits_on_one_hand << endl;
    	cout << endl << endl;
    
    	cout << "Here is the value of the *pointer variable that refers to the digits_one_one_hand variable's memory address: \n" << pointer << endl;
    	cout << endl << endl;
    
    // Goal 4:
    
    	int legs = 0;
    
    	cout << "Please enter the amount of legs that you current have: " << endl;
    	cin >> legs;
    
    	switch(legs){
    
    		case 0:
    			cout << "You are physically handicapped" << endl;
    			break;
    
    		case 1:
    			cout << "You are a pirate! " << endl;
    			break;
    
    		case 2:
    			cout << "You are a normal human being" << endl;
    			break;
    
    		default:
    			cout << "You are not a human being" << endl;
    			break;
    
    	}
    
    
    // Goal 5:
    
    	ofstream file;
    
    	// This is the syntax for creating an object called myfile that allows you to output
    	// or create files on your computer
    
    
    	file.open("brandnewfile.txt");
    
    	// This opens a text file within the project folder, but will create one of that
    	// name if there is not one present already
    
    	file << "This is text that will be written into the document \n";
    
    	// This command will print text into the file we just created
    
    	file.close();
    
    	// This will close the file and also prevent memory leaks
    
    
    // Goal 6:
    
    	char myfilename[50];
    
    	ifstream mynewfile;
    
    	// This ifstream command creates a new object so I can write and read files
    
    	cout << "Please enter the name of the file you wish to open (ex: Note.txt) \n" << endl;
    	cin.getline(myfilename, 50);
    
    	mynewfile.open(myfilename);
    	// This tells the program to open up the file in the project that the user specified
    
    	if(!mynewfile.is_open()){
    
    		exit(EXIT_FAILURE);
    
    		// This allows us to exit the program if the file is not open, != open
    
    	}
    
    	char input[50];
    
    	cout << "Please enter the text you wish to insert into the file: \n" << endl;
    
    
    	mynewfile >> input;
    
    	// This is just inserting the text the user just inputted into the file with the
    	// use of the >> operator
    
    	while(mynewfile.good()){
    
    		cout << input << " ";
    
    		mynewfile >> input;
    
    		// This says, if mynewfile is good aka still open, then write the text
    		// into the document
    
    		// When using the ofstream, think of the file you are using as the beginning
    		// of a cin >> statement, hence the filename, mynewfile >> is used to input
    		// data
    
    	}
    
    
    	system("PAUSE");
    
    
    	return 0;
    
    
    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    It was several years before I worked on a system that offered a debugger, so I had to use other techniques to debug a program. Very early on, I learned a simple debugging technique which we called "trace statements". Basically, I would insert print statements at certain points within the code that would report to me not only that control had reached that point, but also what the values of certain variables were. The idea could also be expanded to incorporate the concept of informing the user when and why the program is about to terminate.

    You wrote (ellipses inserted):
    Code:
    	cout << "Please enter the amount of legs that you current have: " << endl;
    	cin >> legs;
    
     .  .  .
    
    // Goal 6:
    
    	char myfilename[50];
    
    	ifstream mynewfile;
    
    	// This ifstream command creates a new object so I can write and read files
    
    	cout << "Please enter the name of the file you wish to open (ex: Note.txt) \n" << endl;
    	cin.getline(myfilename, 50);
    
    	mynewfile.open(myfilename);
    	// This tells the program to open up the file in the project that the user specified
    
    	if(!mynewfile.is_open()){
    
    		exit(EXIT_FAILURE);
    
    		// This allows us to exit the program if the file is not open, != open
    
    	}
    Please note that you exit the program without ever informing the user of that fact. Please also note that you never report to the user what you read in. Please also note that echoing back out to the user what you think that he had input can be a valuable debugging practice.

    I believe I had already stated that I don't use iostreams because I find it cumbersome and a giant step backwards to Turbo Pascal. However, as I understand cin to work, it basically behaves as scanf does, in that it reads just enough of the input stream to convert the input into the variable values that it is given to convert and then it stops, leaving whatever characters follow in the input buffer. That would include the newline. Furthermore, I understand that getline works by reading every thing in the input buffer until it hits a newline.

    Please notice that the cin operation that precedes the getline to read in the filename happens to be the reading in of a number for legs. Therefore, I believe that when you read in legs, that left a newline in the input buffer. Then when you called getline, it immediately read in that left-over newline, which satisfied it, and, since there is no file in the current directory with the name of empty string, the program exited without notifying you.

    You could place that getline in a loop until it reads in a non-empty string. Or you could see if there's a method for cin that will reset, clear, or flush the input buffer -- like I said, I don't do iostreams, so I don't know whether that exists.

    But at the very least you need to tell the user when you are exiting the program with an error condition. Also outputting the filename that you choked on would be a nice extra.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Ah I see, so adding a for loop for the getline sequence would clear that bit up.

    Also, I should add exiting statements throughout the program to also force a debug for myself.

    Cool, because I ran this without errors, and believe me, I always expect them and always remove them when I can nowadays.

    Then again, I'm trying not to go the video tutorial route and keep on trucking with the book I have on my comp, I think I'm on chapter 3 so far, so its been revealing a lot of minor things I didn't get from videos.

    Thanks a bunch for the reply!

IMN logo majestic logo threadwatch logo seochat tools logo