|
-
July 27th, 2010, 02:06 AM
#1
why is it skipping lines?
I have a function called addChecksum that is called from a switch statement. The function is to ask for a file path to a file, then to open the file. But if it doesn't open, then it should take you back to the menu. Right now, it runs through the entire function without giving me a chance to enter the file path. I don't understand why. Here is my code
Code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
void selection();
void addChecksum();
void runChecksum();
int main()
{
selection();
system("pause");
return 0;
}
void selection()
{
char choice;
cout << "Please Select: " << endl;
cout << " A) Add checksum to specified file" << endl;
cout << " B) Verify integrity of specified file" << endl;
cout << " Q) Quit" << endl;
cin >> choice;
cout << endl;
switch (choice)
{
case 'A':
addChecksum();
break;
case 'a':
addChecksum();
break;
case 'B':
runChecksum();
break;
case 'b':
runChecksum();
break;
case 'Q':
return;
case 'q':
return;
default:
cout << "Your choice did not match any of the menu options." << endl;
}
}
void addChecksum()
{
string inputFileName;
ifstream inputFile;
string line;
cout << "Specify the file path: ";
getline(cin, inputFileName);
inputFile.open(inputFileName.c_str());
cout << endl;
while(!inputFile.is_open())
{
cout << "The file " << inputFileName << "could not be found or opened!" << endl;
selection();
}
}
void runChecksum()
{
string inputFileName;
ifstream inputFile;
string line;
int checksum;
checksum = 0;
while(!inputFile.is_open())
{
cout << "Specify the file path: " << inputFileName;
getline(cin, inputFileName);
inputFile.open(inputFileName.c_str());
}
cout << "File checksum = " << checksum << endl;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|