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

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1

    Of Stream program errors


    Hey guys,

    I made an ofstream program with the help of this tutorial:

    http://www.youtube.com/watch?v=f8yj1Z0MyJY

    It works great, but I wanted to implement a couple lines of code that would return the user back to the main function for them to try to name the file again in the else statement below.

    I have what I added in red that's causing it to loop constantly. Should I include it AFTEr the myfile.close statement?

    Here's my code:

    Code:
    #include <iostream>
    #include <string>
    #include <Windows.h>
    #include <fstream>
    
    // An Of stream basically writes text into a file, pretty cool
    
    using namespace std;
    
    int main()
    {
    	ofstream myfile;
    
    	// This creates an ofstream variable named myfile
    
    	char filename[50];
    	// This creates a character variable named filename
    	// that will store up to 50 characters that the user enters
    
    	string userInput;
    	// This string just holds the user's input
    
    	cout << "Please enter the file name you wish you wish to write on: " << endl;
    
    	cin.getline(filename, 50);
    	// Again, this is pretty much the same thing as cin>>, and it
    	// will store 50 characters that the user inputs into the 
    	// filename variable
    
    	myfile.open(filename);
    	// This opens the file itself
    
    	if(myfile.is_open())
    	{
    	// This function checks if the file is open
    
    		cout << "Enter the text you wish to insert the text file: " <<endl;
    		getline(cin, userInput);
    		// This will store whatever the user types into the 
    		// variable userInput
    		myfile << userInput;
    
    		// Basically, you're just labeling the file's title
    		// with whatever the user just inputted
    
    	}
    
    	else
    		cout << "Sorry, an error has occurred. Please wait 3 seconds and try again" << endl;
    		Sleep(3000);
    		system("CLS");
    		return main();
    
    		// This return statement is from previous tutorials
    		// and actually ripped straight from my music program.
    
    		// This refers to the Windows.h library that contains
    		// the sleep command that will force the computer to pause
    		// for 3 seconds and return the user to the main function
    		// so that the user can try again.
    
    	myfile.close();
    
    	// This closes the file after successfully completing the if
    	// statement
    
    
    	system("PAUSE");
    	return 0;
    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Never call main(). main() is the entry point for the program. The startup code calls main(), but you never do.

    Are you in another function? I cannot tell because your program is an unreadable mess. Use code tags to preserve your program's indenting! 17 posts and you still don't know that? No excuse!

    If you are in another function, then simply return. If you are still in main(), then create a looping control structure such as a do-while.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    By the way, you might also want to put some curly braces {} after the else, otherwise it will only consider the first statement after the keyword as part of the else condition.

    Also, never recursively call main().
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    1
    Okay, thank you so much guys.

IMN logo majestic logo threadwatch logo seochat tools logo