3 Replies - 771 Views - Last Post: 24 May 2012 - 11:21 PM Rate Topic: -----

#1 soconfused  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-May 12

Text file into array, calculations

Posted 24 May 2012 - 05:05 PM

So my assignment is to read the data from a text file into array, then depending on the first letter of the data, perform specific calculations.

inFile:

A 4 12
M 2 5
A 9 3
M 6 8
A 7 1

main program:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "Calcs.h"
#include <string>

using std::ifstream;
using std::ofstream;
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	Calcs myCalcs;

	ifstream inFile ("datafile.txt");
	ofstream outFile ("resultfile.txt");

	string line;

	char ch;
	char A;
	char M;
	int num1;
	int num2;
	int mult;
	int add;

	if (!inFile)
	{
		cerr << "Error:  Input file could not be opened.  Aborting..." << endl;
		exit (1);
	}

	if (!outFile)
	{
		cerr << "Error:  Output file could not be opened.  Aborting..." << endl;
		exit (1);
	}

		
	while ( !inFile.eof() )
	{
		getline (inFile, line);

		inFile >> ch >> num1 >> num2;
		if (ch = A)
		{
			myCalcs.Add2 (num1, num2);
			outFile << num1 << "+" << num2 << "=" << add << endl;
		}
		if (ch = M)
		{
			myCalcs.Multiply2 (num1, num2);
			outFile << num1 << "*" << num2 << "=" << mult << endl;
		}
		
		
	}
	cout << "End-of-file reached..." << endl;

	inFile.close();
	outFile.close();

	return 0;
}



I know this isn't correct. I don't know how to separate the char A or M from the rest of the array. If the line begins with an A, I need to call the Add2 function; and if it begins with an M, the Multiply2 function. I have no idea what I am supposed to do, but I have searched a lot of sites and I can't figure out exactly what I am supposed to do at this point. I don't want the code written for me; I just want someone to tell me what I am doing wrong so I understand it.

Is This A Good Question/Topic? 0
  • +

Replies To: Text file into array, calculations

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Text file into array, calculations

Posted 24 May 2012 - 06:50 PM

Well, for one thing, I don't see an array anyplace in your program.

You should probably begin by studying this tutorial on Arrays thoroughly, from beginning to end.

---------------------------------------------------

For another, assuming you're supposed to be writing a standard C++ console application, not a Windows API application, you should learn how to set up your project as such:

Setting up a project in Visual C++ for ordinary C++ programs (i.e., not Windows GUI applications):


Open the New Project window, select Win32 Console Application, type a project name & click OK

That opens the Application Wizard window. DON'T click Finish. Instead, click Next.

On the next window, under "Additional options", click Empty project. Now click Finish.

You now have a new, empty "console solution" ready to go.

****************************************

Adding files to the project:


The Solution Explorer should be visible, usually in a panel on the left side of your screen. If you don't see it, click View, then click Solution Explorer.

To add a new file to your project (this applies to your initial program .cpp file as well as any additional header and source files you want to add to the project), in the Solution Explorer right-click on Source Files. Point your cursor to Add, then click on New Item, then select the type of file you want to add (for beginners this will usually be a C++ File or a Header file), then near the bottom of the panel enter a name for the file, and finally click Add.

(If you want to add an already-existing file to a project, you can click on Existing Item and use the explorer window that opens to locate and select your file. However, the file should already be located in an appropriate folder for the project. Therefore this method isn't recommended for beginners unless you fully-understand the appropriate directory structure for Visual Studio projects. It's safer to let Visual Studio add a new empty file and then copy and paste your existing code into that new file.)

You can also add a file to the project by clicking Project on the Toolbar at the top of the window, then click Add New Item and then proceed as described above.


You now have an empty file in which to write your program. When ready to test your program, build it by pressing F7, then run it by typing Ctrl-F5 (that means press and hold the Control key while you press the F5 key) and the terminal window will stay open when the program finishes. (Therefore, no need to use system("pause"); or cin.get(); or any of the other "tricks" people use to prevent the program from ending.)
Was This Post Helpful? 1
  • +
  • -

#3 soconfused  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-May 12

Re: Text file into array, calculations

Posted 24 May 2012 - 07:00 PM

I understand how to start a new program (I am using Microsoft Visual Studio 2010), type the code in and run it. I have read the thing on arrays. I know in that code I did not have an array set up because I keep trying new code and none of it has worked so far so I delete it and try again. I have the .txt file already in my program, and I know how to do all that stuff. What I am really confused on is how to get the info from my inFile into the array. The inFile starts with a letter, and depending on what that letter is, tells me what which function to call.
Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,089
  • Joined: 08-January 12

Re: Text file into array, calculations

Posted 24 May 2012 - 11:21 PM

Change line 47 to
if (ch == 'A')

I have used == because = is for assignment, == is for comparison. Plus, I have used 'A' to indicate it is the constant character A, and not a variable. If you to test against the char A you have declared, you must change line 22 to
char A = 'A';

and line 47 to
if (ch == A)

The choice is yours, but == is ALWAYS required when checking a condition in if/for/while.
Also, do a similar thing for 'M'

This post has been edited by aresh: 24 May 2012 - 11:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1