• 10
• 10
• 12
• 12
• 14

# [SOLVED]Just started learning C++, stuck

This topic is 3675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I've just started programming in C++ a few days ago, and have run into a wall. I'm using the things I learn from tutorials to make my own programs to see how functions work. The program I'm working on now is a console program where you can chose to write to a file, or read from it. I've got the reading part down, but when implementing a menu, I can't get the program to loop correctly. Here's the source code for my program:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int c;
char name [20];
string line;
char Prompt;

int main ()
{
do
{
cout << "1. Read from file." << endl << "2. Write to file." << endl;
cin >> c;

do
{
cout << "File to be opened: ";
cin >> name;
ifstream file (name);

if (file.is_open())
{
cout << endl;

while (! file.eof() )
{
getline (file, line);
cout << line << endl;
}
file.close();
cin.get();
}

else
{
cout << "Unable to open file" << endl;
cin.ignore();
cin.get();
}
cout << endl << "Do you want to perform a new operation? (y/n) ";
cin >> Prompt;
}
while (c == 1);

}
while (Prompt == 'y');

return 0;
}


The program starts fine, and when I press 1 I can type the file's name, it opens, the restart command comes up. But, when I press "y", it skips the menu and goes right to the "File to be opened: " screen. I cannot figure out why its doing that, because I checked the braces and they all go to the correct places, so in theory it should go back to the menu, but it does not. Can any of you experts find out whats wrong? [Edited by - sjheiss on April 3, 2008 10:58:07 PM]

##### Share on other sites
Well, first step is to clean up your code:

int main () {	do	{		cout << "1. Read from file." << endl << "2. Write to file." << endl;		cin >> c;		do		{			//read file			cout << "File to be opened: ";			cin >> name;			ifstream file( name );			if ( file.is_open( ))			{				cout << endl;				while ( !file.eof() )				{					getline (file, line);			        cout << line << endl;				}				file.close();				cin.get();			}			else 			{				cout << "Unable to open file" << endl;				cin.ignore();				cin.get();			}	  			cout << endl << "Do you want to perform a new operation? (y/n) ";			cin >> Prompt;					} while ( c == 1 );		} while ( Prompt == 'y' );	    return 0;}

Can you see the error(s) now?

(Take the time to format your code, it will be easier on the eyes; I learned that the hard way!)

##### Share on other sites
Quote:
 Original post by _fastcallWell, first step is to clean up your code:*** Source Snippet Removed ***Can you see the error(s) now?(Take the time to format your code, it will be easier on the eyes; I learned that the hard way!)

Yeah, I tried formatting it in the beginning, but I kept adding and changing stuff that it got messed up :D

WAIT, I think I got it. If I'm right, variable "c" is still storing 1, so it's bypassing the menu! I'm gonna fix that and see if it works :D

EDIT: Damn, you can only delete pointers :(
How do I clear out the value of a type? Also, just found out that when I press "n" at the restart prompt, it restarts anyways, and it didn't before I added the menu :P

##### Share on other sites
Here's one problem:

	do	{		// ...		do		{			// ...			cout << endl << "Do you want to perform a new operation? (y/n) ";			cin >> Prompt;		} while (c == 1);	} while (Prompt == 'y');

The variable c isn't changed in this loop, what you probably wanted to do is this:

	do	{		cout << "1. Read from file." << endl << "2. Write to file." << endl;		cin >> c;							if ( c == '1' )		{			// read file		}		else if ( c == '2' )		{			// write file		}		else		{			cout << "Invalid selection" << endl;		}				cout << endl << "Do you want to perform a new operation? (y/n) ";		cin >> Prompt;			} while ( Prompt == 'y' );

EDIT:
Quote:
 Original post by sjheissEDIT: Damn, you can only delete pointers :(How do I clear out the value of a type?

Set it to a value that won't work in your program, such as '0'.

##### Share on other sites
Quote:
Original post by _fastcall
Here's one problem:

*** Source Snippet Removed ***

The variable c isn't changed in this loop, what you probably wanted to do is this:

*** Source Snippet Removed ***

EDIT:
Quote:
 Original post by sjheissEDIT: Damn, you can only delete pointers :(How do I clear out the value of a type?

Set it to a value that won't work in your program, such as '0'.

Freakin awesome! You rock :D

Thanks for the help, really appreciate it. I'm doing some programming electives next year in high school, so I wanted to get a head start, so I can be ahead in the class :D

##### Share on other sites
No problem, be sure to prefix your first post's title with "[SOLVED]" so others will know, and be sure to rate people who have helped (wink, wink) ^^

##### Share on other sites
Quote:
 Original post by _fastcallNo problem, be sure to prefix your first post's title with "[SOLVED]" so others will know, and be sure to rate people who have helped (wink, wink) ^^

Ok, I'll be suer to do that on new topics, and I didn't forget :D