If I have in a text file a word followed by numbers, how would I read only the numbers?
Ex:
Add 5645635654653463546.

create an ifstream object and use its >> operator to skip the first word and read the second.

ifstream in("file");
string word;
in >> word; // read "Add"
in >> word; // read the digits

create an ifstream object and use its >> operator to skip the first word and read the second.

ifstream in("file");
string word;
in >> word; // read "Add"
in >> word; // read the digits

so inputting word twice will just skip the first string? I don't understand how that works...

the >> operator first skips all leading space then reads all the characters up to the next space. So if the file contains " Add 5645635654653463546" the >> operator reads Add into the string word. Then the next line the >> operator erases what it in word and reads the next non-space characters into word

It you are still confused maybe you should write a small program to verify what is happening

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream in("somename.txt");
    string word;
   in >> word;
   cout << word << "\n";
   in >> word;
   cout << word << "\n";
}

I've made a program similar to yours. It compiles and runs but the console is empty. Are we suppose to use ofstream?

I've made a program similar to yours. It compiles and runs but the console is empty. Are we suppose to use ofstream?

ofstream will produce output to a file, not the console. cout is from iostream and will display to the console. That's what Ancient Dragon is doing here in lines 11 and 13.

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream in("somename.txt");
    string word;
   in >> word;
   cout << word << "\n";
   in >> word;
   cout << word << "\n";
}

However he did forget one important thing at the end of his program. :)

return 0;

However he did forget one important thing at the end of his program. :)

return 0;

Nope, I didn't forget it. Its optional. return 0 is the default if nothing is specified. That was a fairly recent addition to the c++ standards so some older compilers may require the return statement.

Comments
You are correct.

Nope, I didn't forget it. Its optional. return 0 is the default if nothing is specified. That was a fairly recent addition to the c++ standards so some older compilers may require the return statement.

Interesting. I thought it was along the lines of using void main () , but it isn't. Just wrote a couple of programs. Dev C++ accepted int main () with no return statement, but not void main () . I thought I had caught you in a rare slip-up, but I guess not. ;)

Okay, I'm working on the big part now.

I've got to put together a calculator sort of program. The calculator works by reading a text file provided by the user that is filled with commands. The program then outputs those files into a new text file named by the user.

Here is a sample of how it works

if commands.txt reads : \
Add 2100000

Add 500

Add 1001

out.txt or whatever the user names the output file will read:

Adding 2100000
Result: 2100000

Adding 500
Result: 2100500

Adding 1001
Result: 2101501

I was told to use an array to go through the digits one by one after converting them from strings to int. I'm working on the addition right now and I'm sort of stuck.

Here is what I've come up with so far.

#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

void DoAddition( string charNumber[] );
void ReadCommand( void );
string GetString( string Prompt );
void HoldScreen( void );

const int SIZE = 5000;

int main()
{	

	string charNumber[SIZE];
	ofstream ResultFile;
	ReadCommand();

	do
	{
		ResultFile.open( GetString( "Enter the file for output--> " ).c_str() );
		if ( ResultFile ) break;
		cout << "Unable to open specified file. " << endl << endl;
		ResultFile.clear();
	}while (true);

	
	


}
void ReadCommand(void)
{
	ifstream CommandFile;
	do
	{
		CommandFile.open( GetString( "Enter the file containing commands--> " ).c_str() );
		if ( CommandFile ) break;
		cout << "Unable to open specified file. " << endl << endl;
		CommandFile.clear();
	}while (true);

	string Command;
	string charNumber[SIZE];
	CommandFile >> Command >> charNumber[SIZE];
	if ( Command == "add" )
		DoAddition();
}

void Addition( string charNumber[], int Number[] )
{
	
	for ( int i = 0 ; i <= SIZE ; ++i )
		 Number[i] = charNumber[i] - '0';
	sum%10 = Number[SIZE-i];



}

string GetString( string Prompt )
{
	do
	{
		string Response;
		cout << Prompt;
		cin >> Response;
		cin.ignore( 99, '\n' );
		if ( cin )
			return Response;
		cout << "Bad input, please try again." << endl;
		cin.clear();
		cin.ignore( 99, '\n' );

	} while ( true );
}

Not sure what you are trying to do in the Addition function. I think you need to split the problem into two parts. Don't convert from string to integer and at the same time do arithmetic. Set up a funtion:

int StringToInt (string stringNum)
{
     int numericRep = 0;
     for (int i = 0; i < stringNum.length (); i++)
     {
          // go through stringNum one character at a time, extract
          // it, convert it to a digit, then adjust numericRep to use this 
          // digit
     }

     return numericRep;
}

Do this conversion first, then the arithmetic. There are also the atoi and itoa functions.

I was told to use stringnum - '0' to convert from string to int. Is there no way to do both in just one function?

I was told to use stringnum - '0' to convert from string to int.

Not sure what you are trying to do there but you can't do something like this if you were trying to do the entire conversion in one fell swoop.

int num = stringnum - '0';

where stringnum is a string. You can do this:

int digit = stringnum[i] - '0';

which is subtracting a character from a character rather than a character from a string. That's kind of what I had in mind when I posted the loop.

Is there no way to do both in just one function?

Sure you can do it in one function. But you can't do what you did in lines 5 and 6:

void Addition( string charNumber[], int Number[] )
{
	
	for ( int i = 0 ; i <= SIZE ; ++i )
		 Number[i] = charNumber[i] - '0';
	sum%10 = Number[SIZE-i];



}

In line 5, it looks like you are subtracting a char from a string, which you can't do to convert a number. In line 6, I'm not sure what you are trying to do, but I'm guessing you want the % on the right hand side of the equation. I'm not sure what you are trying to accomplish in this function and what the parameters passed to it represent, whether Number is a digit or the full number or what. I don't see any addition in this function. I suggested splitting it into two functions and naming them more descriptively so it's easier to debug and follow the program. It can certainly be done in one function.

Well the way the arithmetic is done for the purposes of this program is one int at a time. So if you have a string of numbers like so- 654646346 and you want to add 9999999 you start from the right and work your way left. 6 goes into 9, but thats 15, can't have 15 on one spot. So we put a 5 down in that spot and carry the ten to the next spot. Sorta like elementary arithmetic. That's what I'm trying to get at with the mod operator. I'm just entirely lost with this, though, heh.

Well the way the arithmetic is done for the purposes of this program is one int at a time. So if you have a string of numbers like so- 654646346 and you want to add 9999999 you start from the right and work your way left. 6 goes into 9, but thats 15, can't have 15 on one spot. So we put a 5 down in that spot and carry the ten to the next spot. Sorta like elementary arithmetic. That's what I'm trying to get at with the mod operator. I'm just entirely lost with this, though, heh.

#include <string>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

void DoAddition( string charNumber[] );
void ReadCommand( void );
string GetString( string Prompt );
void HoldScreen( void );

const int SIZE = 5000;

int main()
{	

	string charNumber[SIZE];
	ofstream ResultFile;
	ReadCommand();

	do
	{
		ResultFile.open( GetString( "Enter the file for output--> " ).c_str() );
		if ( ResultFile ) break;
		cout << "Unable to open specified file. " << endl << endl;
		ResultFile.clear();
	}while (true);

	
	


}
void ReadCommand(void)
{
	ifstream CommandFile;
	do
	{
		CommandFile.open( GetString( "Enter the file containing commands--> " ).c_str() );
		if ( CommandFile ) break;
		cout << "Unable to open specified file. " << endl << endl;
		CommandFile.clear();
	}while (true);

	string Command;
	string charNumber[SIZE];
	CommandFile >> Command >> charNumber[SIZE];
	if ( Command == "add" )
		DoAddition();
}

void Addition( string charNumber[], int Number[] )
{
	
	for ( int i = 0 ; i <= SIZE ; ++i )
		 Number[i] = charNumber[i] - '0';
	sum%10 = Number[SIZE-i];



}

string GetString( string Prompt )
{
	do
	{
		string Response;
		cout << Prompt;
		cin >> Response;
		cin.ignore( 99, '\n' );
		if ( cin )
			return Response;
		cout << "Bad input, please try again." << endl;
		cin.clear();
		cin.ignore( 99, '\n' );

	} while ( true );
}

Line 46 - You've already declared this on line 17. What you are declaring on line 46 is a local array. It has no meaning outside of the ReadCommand function. Do you want this?

Line 47 - You immediately use the array you just defined using a subscript that is not allowable for this array. Line 46 defines the array for indexes 0 through SIZE - 1. You can't use SIZE as an array index for this array. It is not large enough.

Line 55 - SIZE is 5000. What does the 5000 represent here? You're going through this loop 5000 times why? If it is one trip through the loop for every digit in a number, that's way too many trips through the loop for adding one integer to another integer.

Lines 21 - 27 - What does this do-while loop do?

Line 19 - You are only calling ReadCommand once here. It's not in a loop. There are no loops in ReadCommand either. It only tries to read two bits of data, but your file looks much bigger than that and it looks like you want to read in the whole thing.

Lines 7, 49, 52 - There's a declaration fro DoAddition (line 7), a call to DoAddition (line 49), but no DoAddition function. Line 52 is a function called Addition, but there's no call to it and no declaration for it. I'm guessing this is supposed to be DoAddition, not Addition.


I think you are going to have to document this program better because there are problems and I can't tell what you are trying to do and thus can't offer any more suggestions than I already have. Also, if you document and make a firm decision on what each variable represents and what each function represents and what each function's job is, it's almost guaranteed that this will help you figure out what needs to be done, so it helps you in addition to helping us.

I'll see if I can just start all over using what you gave me so far. Would probably be the best thing to do. Thanks for your help.

This article has been dead for over six months. Start a new discussion instead.