hi im new to the forums and im stuck on a part of my code that i need to do for homework

in the code below in my loadPuzzle function it seems to skip my cin.getline and i dont know why.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

enum menu {load=1,solve,create,quit};

int menu();
void loadPuzzle(ifstream&);
void printUnsolvedPuzzle(ifstream&);

int main()
{
        ifstream file;
        bool exit = false;
        while (exit != true)
        {
            switch (menu())
            {
                case load:
                    loadPuzzle(file);
                    printUnsolvedPuzzle(file); //ignore this part for now
                    break;
                case solve:
                    cout << "Empty" << endl;
                    cout << endl;
                    break;
                case create:
                    cout << "Empty" << endl;
                    cout << endl;
                    break;
                case quit:
                    cout << "Quiting program" << endl;
                    exit = true;
                    break;
                default:
                    cout << "Error. Invalid input." << endl;
            }
        }
    return 0;
}

int menu()
{
    int choice;
    
    cout << "***********************\n"
            "*                     *\n"
            "*     Find a Word     *\n"
            "*                     *\n"
            "***********************" << endl;
         
    cout << "1. Load Puzzle \n"
            "2. Solve Puzzle \n"
            "3. Create Puzzle \n"
            "4. Quit Program \n"
             "Choice: ";
    cin >> choice;
    cout << endl;
    return choice;
         
}
         
    
void loadPuzzle (ifstream& file)
{
    char fileName[30];
    cout << "Enter a file name: ";
    file.getline(fileName,'\n');
    file.open(fileName);
}

void printUnsolvedPuzzle(ifstream& file) //ignore this part
{
    char myString[80];
    while(!file.eof())
    {
        file.getline(myString,sizeof(myString));
        cout << myString << endl;
    }
    file.close();
}

i have a feeling its to do with my switch statement

Recommended Answers

All 7 Replies

nevermind i figured it out sorry to bother everyone
all i need to do was change
file.getline(fileName,'\n');
to
cin >> fileName

Are you sure your code works fine??

well its incomplete but for now it works quite fine.
btw is there a way to read from a text until u hit a certain character?
ive tried
file.getline(myString,';');

dont think its right since i get thrown into an infinite loop

#include <iostream>
int main()
{
	
	char str[10];
	std::cout<<"Enter a string:";
	std::cin.getline(str,10);
	std::cout<<str;
	return 0;
}

oh sorry i kinda screwed up my question. kinda tired.
im reading data from a file and i want to stop reading once the program finds a ';'
im thinking that i cant use the getline function and i need to use the get function to look at each individual character.


EDIT: err nevermind i kinda figured it out again. sorry for bothering everyone

oh thats right one of my main problems
in my menu function when i enter a char instead of an int it throws me into an infinite loop wondering how that is fixed??

sorry for double posting

Check the return value of cin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.