GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-May-2008, 19:53
gagless12's Avatar
gagless12 gagless12 is offline
New Member
 
Join Date: Apr 2008
Posts: 14
gagless12 is on a distinguished road

Working with strings and arrays.


Hi everyone. Back again, unfortunately .

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.

CPP / C++ / C Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
 
void DoAddition( string charNumber[], int Number[] );
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 Number;
    CommandFile >> Command >> Number;
    if ( Command == "add" )
        DoAddition();
}
 
void Addition( string charNumber[], int Number[] )
{
 
    for ( int i = 0 ; i <= SIZE ; ++i )
         charNumber[i] - '0' = Number[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 );
}
Last edited by admin II : 09-May-2008 at 04:34. Reason: Changed [CODE] to [CPP]
  #2  
Old 07-May-2008, 23:13
zatora zatora is offline
New Member
 
Join Date: May 2008
Posts: 25
zatora is on a distinguished road

Re: Working with strings and arrays.


i am not sure if this is what u need if it is not good enough u may wanna post the problem with more details, i am new my self to c++
CPP / C++ / C Code:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void addition(ifstream& infile,ofstream& outfile,int& num);
int main()
{
ifstream indata;
ofstream outdata;
int number =0;
indata.open("data.txt");
outdata.open("output.txt");
addition(indata,outdata,number);
indata.close();
outdata.close();
return 0;
}
void addition(ifstream& infile, int& num)
{
int sum =0;
while(!infile.eof())
{
infile>>str1>>num;
sum =sum+num;
outfile<<str1+"ing"<<num<<endl;
outfile<<"Result :"<<sum<<endl;
}
}
  #3  
Old 09-May-2008, 14:04
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Working with strings and arrays.


Quote:
Originally Posted by gagless12
Hi everyone. Back again, unfortunately .

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.

CPP / C++ / C Code:
snipped for space
Why is it new posters rely on our psychic powers to figure out what stuck means? Can't you tell us what you are stuck on? What doesn't work? Maybe what your problem is? I take it the Guidelines weren't explicit enough.


One thing I notice is you call the DoAddition() function with no parameters. How does the function know what to add? And where is it? It's not even defined.

To program this easier, from main()
1) Call a function to simply read the file and return with the line read as a string
2) Call a convert function to return the number portion as an integer value
3) Call a calculate function to perform the calculation, return the total
4) Call an output function
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #4  
Old 14-May-2008, 12:13
gagless12's Avatar
gagless12 gagless12 is offline
New Member
 
Join Date: Apr 2008
Posts: 14
gagless12 is on a distinguished road

Re: Working with strings and arrays.


I have an insert function using an array with size 5. I want to throw as many numbers as I want into it only keeping the most recent five.

How would I move everything back in the array one spot so I could drop off the first element and put the new element in the fifth spot?

I was thinking something like
CPP / C++ / C Code:
if ( Size >= MAX_SIZE )
		Data[0] = Data[1]
		Data[1] = Data[2]
		Data[2] = Data[3]
		Data[3] = Data[4]
		Data[4] = NewData;
 

I don't even know if that works, but if it does, is there a cleaner way to do it?
  #5  
Old 14-May-2008, 12:21
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Working with strings and arrays.


Quote:
Originally Posted by gagless12
I don't even know if that works, but if it does, is there a cleaner way to do it?
Yes, that would work. If you want it cleaner, try putting it in a for-loop.

CPP / C++ / C Code:
for(int i=0;i<MAX_SIZE-1;++i) Data[i] = Data[i+1];
Data[MAX_SIZE-1] = NewData;
  #6  
Old 14-May-2008, 14:37
zatora zatora is offline
New Member
 
Join Date: May 2008
Posts: 25
zatora is on a distinguished road

Re: Working with strings and arrays.


it is going to be a for loop as i see fakepoo did but if u assignement post un example like if u wanna insert five new entry at the time so i can figure out what we need to do exactly do u wanna integrate this in a unction or the main() will take of it
  #7  
Old 14-May-2008, 18:29
gagless12's Avatar
gagless12 gagless12 is offline
New Member
 
Join Date: Apr 2008
Posts: 14
gagless12 is on a distinguished road

Re: Working with strings and arrays.


Okay, now if I was to continuously add numbers to the array and at any given time I wanted to know which element was the newest element in the array, I would just search the array right?
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Somebody help with codes for Strings and Arrays raslou Java Forum 1 04-Nov-2006 15:35
? re strings as character arrays function earachefl CPP / C++ Forum 9 25-Apr-2006 16:59
Dynamic vs Static Arrays WaltP Miscellaneous Programming Forum 5 16-Feb-2006 15:54
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 22-Nov-2004 23:26
Strings and arrays in C crystalattice C Programming Language 5 11-May-2004 19:01

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.