Need help with c++ concept

Sep 6, 2011 at 8:39am
ok so we are learning I/O with files. One input file is Data.txt and it contains this:
Robby Tono
5 15
Tim Con
2 13.5
Bella Quint
10 5

and once the output file is created to another .txt file (e.g Ouput.txt) this is displayed.
Robby Tono $75
Tim Con $27
Bella Quint $50

what particular code was done so this "conversion" can happen? Please help
8
Additional Details
Yes, they are multiplied(pretty obvious), but this is done with files. One file is read (DATA.TXT) and contains the information above. And then another file is -Created- output(OUTPUT.TXT) with the names and multiplication like stated above. My question is what code was used to do this?
Last edited on Sep 6, 2011 at 8:40am
Sep 6, 2011 at 11:36am
closed account (zb0S216C)
Such an operation is simple. Here's how I would go about it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <fstream>
#include <string>
#include <Windows.h>

struct Datum
{
    Datum( );

    std::string FirstName, SecondName;
    int Multiplier, BaseValue;
};

Datum::Datum( )
    : FirstName( "John" ), SecondName( "Doe" ), Multiplier( 0 ), BaseValue( 0 ) 
{ }

bool ExtractDatum( Datum &, std::ifstream & );

int main( )
{
    bool ExtractFailed( false );
    std::ifstream IStream( "MyFile.txt" );

    if( !IStream.is_open( ) )
        return 1;

    Datum Datums[3];

    for( short i( 0 ); i < 3; i++ )
    {
        if( !ExtractDatum( Datums[i], IStream ) )
        {
            MessageBox( nullptr, L"Extraction failed. Check the file format.", L"Extraction Error", 
                        ( MB_OK | MB_ICONERROR ) );
  
            ExtractFailed = true;
            break;
        }
    }

    if( ExtractFailed )
    {
        // Handle failure...
        return 1;
    }
    
    // Manipulate the Datums...

    return 0;
}

bool ExtractDatum( Datum &Data, std::ifstream &Stream )
{
    if( !Stream.is_open( ) )
        return false;

    Stream >> Data.FirstName;
    Stream >> Data.SecondName; 
    Stream >> Data.Multiplier;
    Stream >> Data.BaseValue;

    return true;
}


Wazzak
Last edited on Sep 13, 2011 at 12:16am
Sep 10, 2011 at 12:11am
I still haven't learned structures or datums.

I need to use call functions, ofstream and ifstream.

i got lost with your code lol thanks though
Sep 10, 2011 at 2:05am
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::ifstream input("data.txt");
std::ofstream output("output.txt");

if (input)
{
    std::string name;
    double a, b;

    while (std::getline(input, name) && input >> a >> b)
    {
        output << name << " $" << a * b << std::endl;
        input.ignore();
    }
}
Sep 10, 2011 at 5:34am
Lol, Framework... "Such an operation is simple." LMFAO!
Sep 13, 2011 at 12:15am
closed account (zb0S216C)
:) Why was that so funny, Mathhead?

Wazzak
Topic archived. No new replies allowed.