2 Replies - 407 Views - Last Post: 14 January 2013 - 09:37 AM Rate Topic: -----

#1 gibson_junk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 24-July 09

C++ Reorganizing a open file word by word line by line

Posted 13 January 2013 - 09:03 PM

Been a long time but need to get back into this. I am trying to write a program to make my life easier. The first issue I am having is remembering how to make a simple loop.

Basically I need the CLI to open and prompt for a file location. Verify the file exists and output its contents on the screen. And if the file does not exist then I want it to prompt again for the file location. And if the location is good but they loaded the wrong file and they yes it is not correct how to loop back asking for filename I tried and tried but I am just so far out of C++ that I am not getting it.

File looks like this

Zachary CAD Stephen
Billy BAD Manners

take those lines and arrange like this

NC= Zachary CAD Stephen
AccountDisp: Stephen CAD Zachary
DC= Zachary CAD Stephen

NC= Billy BAD Manners
AccountDisp: Manners BAD Billy
DC= Billy BAD Manners

// ADIT.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

#include <iostream>
#include <istream>
#include <fstream>
#include <string>

using namespace std;

string FileName;
string Contents;
string szAnswer;

ifstream infile;

int _tmain(int argc, _TCHAR* argv[])
{
	SetConsoleTitle(L"Simple");

	bool Correct = false;

	do
	{
		cout << "Enter the file name:  ";
        getline(cin, FileName);
        infile.open (FileName, ifstream::in|ios::out);

		if (infile.good())
		{
			while ( !infile.eof() )
			{
			getline(infile, Contents); // Saves the line in STRING.
			cout << Contents << endl; // Prints our STRING.
			}

			cout << "\nIs this correct? ";
			getline(cin, szAnswer);

			if (szAnswer == "Y" || "y" )
			{
				Correct = true;
				cout << "\nReading file......\n" ;
				system("Pause");
			}
		}
		else
			cout << "File not found!!\n";
	}
	while(!Correct);

	return 0;
}


This post has been edited by gibson_junk: 14 January 2013 - 06:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Reorganizing a open file word by word line by line

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3848
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: C++ Reorganizing a open file word by word line by line

Posted 14 January 2013 - 08:29 AM

I suggest you start by reading this tutorial: C++ tutorial to refamiliarize yourself with the language. There are really too many problems with the code you provided to actually help you fix the program.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 gibson_junk  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 24-July 09

Re: C++ Reorganizing a open file word by word line by line

Posted 14 January 2013 - 09:37 AM

Your right, Some much wrong with it. Post new code soon. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1