Questions

Pages: 1234
It is expected behavior. By default files are truncated, all their previous content is deleted.

Specify that you want append information to file:
1
2
3
4
5
int main()
{
    std::ofstream pushups("PushUps.txt", std::ios::app); //opens file in append mode
    pushups << "Hi!\n";
} //pushups are closed here. No manual closing nessesary. 
So.. i don't have to type pushups.close() ?

I included ios::app and now works.

ifstream ipushups("PushUps.txt", std::ios::app);

int arrayday[9];

while(ipushups >> day)

arrayday[day];

cout << arrayday[2];

I will expect from it to cout 3(in .txt is something like this day and the pushups
ex : 1 33
2 44
3 55
I can't figure it out..
arrayday[day];What this line is for?
I am askind what is this for. I see that it does absolutely nothing because it is tha same as for example:
1
2
3
int i = 42;
//...
i;
I am more interested in what you think it is doing, and what do you want.
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
#include <iostream>
#include <fstream>

int main()
{

    int push, day;

    std::cout << "Enter the number of the day\nIf this it's the first time you are running the program you should enter 1\n";
    std::cin >> day;

    std::cout << "Enter how many Push Ups you did today\n";
    std::cin >> push;

    {
        std::ofstream pushups( "PushUps.txt", std::ios::app ); // open for output
        pushups << day << ' ' << push << '\n';
    }  // the file is closed here; we do not need it to be open any longer

    //ifstream ipushups( "PushUps.txt", std::ios::app );
    std::ifstream ipushups( "PushUps.txt" ); // open for input; no std::ios::app

    const int MAX_DAYS = 9 ;
    int arraypush[MAX_DAYS];
    int arrayday[MAX_DAYS];

    int n = 0 ;
    while( n < MAX_DAYS && ipushups >> day >> push )
    {
        // arrayday[day];
        // arraypush[push];
        arrayday[n] = day ;
        arraypush[n] = push ;
        ++n ;
    }

    // if( n > 2 ) std::cout << arrayday[2] << ' ' << arraypush[2] << '\n' ;
    for( int i = 0 ; i < n ; ++i ) std::cout << arrayday[i] << ' ' << arraypush[i] << '\n' ;
}

I told you at the end of the program.. i expected to write the third line...

I see that it does absolutely nothing because it is tha same as for example:
int i = 42;
//...
i;


Well.. i know that , i asked you how can i fill the array with the information of the txt file..


ED: Thanks Borges.
Last edited on
#include <iostream>
#include <fstream>
#include <windows.h>

void statistics();
void finaldec();

void cls()
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
        return;
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ) ||
            !GetConsoleScreenBufferInfo( hConsole, &csbi ) ||
            !FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
        return;
    SetConsoleCursorPosition( hConsole, coordScreen );
}

int main()
{

    int push, day, dec;


    std::cout << "Enter the number of the day\nIf this it's the first time you are running the program you should enter 1\n";
    std::cin >> day;

    std::cout << "Enter how many Push Ups you did today\n";
    std::cin >> push;
    cls();
    std::cout << "The statistics are:" << std::endl;

    {
        std::ofstream pushups( "PushUps.txt", std::ios::app );
        pushups << day << ' ' << push << '\n';
    }
    std::ifstream ipushups( "PushUps.txt" );

    const int maxday = 10 ;
    int arraypush[maxday];
    int arrayday[maxday];
    int n = 0;

    while( n < maxday && ipushups >> day >> push )
    {
        arrayday[n] = day ;
        arraypush[n] = push ;
        ++n ;
    }
    for( int i = 0 ; i < n ; ++i )
    {
        std::cout << "Day " << arrayday[i]<< ":" << ' ' << "PushUps = " <<arraypush[i] << '\n' << '\n';
    }

    std::cout << "Press a lower number than 9 to close, or 9 to reset the program.\n(If you have completed 10 days press 10)" << std::endl;
    std::cin >> dec;

    switch(dec)
    {

    case 10:
    {
        statistics();
        break;
    }
    case 9:
    {
        std::ofstream ofs;
        ofs.open("PushUps.txt", std::ofstream::out | std::ofstream::trunc);
        ofs.close();
        cls();
        std::cout << "The program has been cleared\n";
        break;
        std::cin >> n;
    }
    }
}

void statistics()
{
    int findec;
    int average[10];
    int dummy[10];

    std::ifstream pushcalc("PushUps.txt");

    for(int i =0; i<10; ++i)
    {
        pushcalc >> dummy[i] >> average[i];
    }
    pushcalc.close();
    cls();
    std::cout << "In 10 days you made " << average[0]+average[1]+average[2]+average[3]+average[4]+average[5]+average[6]+average[7]+average[8]+average[9]<< " Push Ups" << std::endl;
    std::cout << "Write 69 to see your average of Push Ups" << std::endl;
    std::cin >> findec;

    if(findec==69)
    {
        finaldec();
    }
    else {}

}

void finaldec()
{
    cls();
    float average[10];
    int dummy[10];

    std::ifstream pushcalc("PushUps.txt");

    for(int i =0; i<10; ++i)
    {
        pushcalc >> dummy[i] >> average[i];
    }
    pushcalc.close();

    float avr = average[0]+average[1]+average[2]+average[3]+average[4]+average[5]+average[6]+average[7]+average[8]+average[9];

    std::cout << "Your average Push Ups /day is " << avr/10 << std::endl;

}



I think it's finished, but it has some bugs...
Last edited on
it has some bugs...

If it has bugs, then it's not finished.

One problem that I see is that if you don't have 10 records in the file, your read from pushcalc (in finaldec) fails and you have garbage entries in your average array.

It makes more sense to compute a running sum as you go along in your read loop that to add the individual array entries when you declare avr.

Please use code tags, not quotes when posting your code.
http://v2.cplusplus.com/articles/jEywvCM9/



If it has bugs, then it's not finished.

It's version 0.99

One problem that I see is that if you don't have 10 records in the file, your read from pushcalc (in finaldec) fails and you have garbage entries in your average array.


How can i verify if PushUps.txt has 10 average terms ?

It makes more sense to compute a running sum as you go along in your read loop that to add the individual array entries when you declare avr.


Example ?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void finaldec()
{  int dummy;
    int pushups;
    int num_days = 0;
    float sum = 0;

    std::ifstream pushcalc("PushUps.txt");
    while (pushcalc >> dummy >> pushups)
    {  sum += pushups;
        num_days++;
    }
    pushcalc.close();
    float avr = sum/num_days;
    std::cout << "Your average Push Ups /day is " << avr << std::endl;
}


That's v. 1.12, what do you think?

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <windows.h>

void statistics();

void cls()
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
        return;
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ) ||
            !GetConsoleScreenBufferInfo( hConsole, &csbi ) ||
            !FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
        return;
    SetConsoleCursorPosition( hConsole, coordScreen );
}

int main()
{
    int push, day, dec;

    std::cout << "Enter the number of the day\nIf this it's the first time you are running the program you should enter 1\n";
    std::cin >> day;

    std::cout << "Enter how many Push Ups you did today\n";
    std::cin >> push;
    cls();
    std::cout << "The statistics are:" << std::endl;
    {
        std::ofstream pushups( "PushUps.txt", std::ios::app );
        pushups << day << ' ' << push << '\n';
    }
    std::ifstream ipushups( "PushUps.txt" );

    const int maxday = 10 ;
    int arraypush[maxday];
    int arrayday[maxday];
    int n = 0;

    while( n < maxday && ipushups >> day >> push )
    {
        arrayday[n] = day ;
        arraypush[n] = push ;
        ++n;
    }
    for( int i = 0 ; i < n ; ++i )
    {
        std::cout << "Day " << arrayday[i]<< ":" << ' ' << "PushUps = " <<arraypush[i] << '\n' << '\n';
    }
    std::cout << "Press a lower number than 9 to close, or 9 to reset the program.\n(Press 10 for more statistics)" << std::endl;
    std::cin >> dec;

    switch(dec)
    {
    case 10:
    {
        statistics();
        break;
    }
    case 9:
    {
        std::ofstream ofs;
        ofs.open("PushUps.txt", std::ofstream::out | std::ofstream::trunc);
        ofs.close();
        cls();
        std::cout << "The program has been cleared\n";
        break;
        std::cin >> n;
    }
    }
}

void statistics()
{
    int dummy, pushups, days=0;
    float sum;

    std::ifstream pushcalc("PushUps.txt");

    while(pushcalc >> dummy >> pushups)
    {
        sum = sum+pushups;
        days++;
    }
    cls();
    std::cout << "In " << days << " days you made " << sum << " Push Ups" << std::endl;

    while (pushcalc >> dummy >> pushups)
    {
        sum += pushups;
        days++;
    }
    pushcalc.close();
    float avr = sum/days;

    std::cout << "Your average Push Ups /day is " << avr << std::endl;
}
Line 82: You don't initialize sum. It will contain garbage.

Lines 94-98: No need for a second loop. sum and days still have the correct values to calculate avr. In addition, this loop not execute. When you exit the loop at line 90, you're positioned at eof, therefore the read at line 94 will fail immediately.


Can i delete from pushups.txt just the pushups ?


ex : 1 20
2 30

I want to delete just the pushups , and the day to remain...


EDIT: ( v. 1.21)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <fstream>
#include <windows.h>

void statistics();
void start();

void cls()
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;
    if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
        return;
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ) ||
            !GetConsoleScreenBufferInfo( hConsole, &csbi ) ||
            !FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
        return;
    SetConsoleCursorPosition( hConsole, coordScreen );
}

int main()
{
    int decision;

    std::cout << "Enter 1 to start a new day.\nEnter 2 to see the statistics.\nEnter 3 to reset the program\nAnother number to exit." << std::endl;
    std::cin >> decision;

    switch(decision)
    {
    case 1:{
    start();
    break;
    }
    case 2:{
    statistics();
    break;
    }
    case 3:{
    std::ofstream ofs;
        ofs.open("PushUps.txt", std::ofstream::out | std::ofstream::trunc);
        ofs.close();
        cls();
        std::cout << "The program has been cleared\n";
        break;
        std::cin >> decision;
    }
  }
}

void start()
{
    cls();
    int push, day, dec;

    std::cout << "Enter the number of the day\nIf this it's the first time you are running the program you should enter 1\n";
    std::cin >> day;
    std::cout << "Enter how many Push Ups you did today\n";
    std::cin >> push;
    cls();
    std::cout << "The statistics are:" << std::endl;
    {
        std::ofstream pushups( "PushUps.txt", std::ios::app );
        pushups << day << ' ' << push << '\n';
    }
    std::ifstream ipushups( "PushUps.txt" );

    const int maxday = 10 ;
    int arraypush[maxday];
    int arrayday[maxday];
    int n = 0;

    while( n < maxday && ipushups >> day >> push )
    {
        arrayday[n] = day ;
        arraypush[n] = push ;
        ++n;
    }
    for( int i = 0 ; i < n ; ++i )
    {
        std::cout << "Day " << arrayday[i]<< ":" << ' ' << "PushUps = " <<arraypush[i] << '\n' << '\n';
    }
    std::cout << "Press a lower number than 9 to close, or 9 to reset the program.\n(Press 10 for more statistics)" << std::endl;
    std::cin >> dec;

    switch(dec)
    {
    case 10:
    {
        statistics();
        break;
    }
    case 9:
    {
        std::ofstream ofs;
        ofs.open("PushUps.txt", std::ofstream::out | std::ofstream::trunc);
        ofs.close();
        cls();
        std::cout << "The program has been cleared\n";
        break;
        std::cin >> n;
    }
    }
}

void statistics()
{
    int dummy, pushups, days=0;
    float sum =0;

    std::ifstream pushcalc("PushUps.txt");

    while(pushcalc >> dummy >> pushups)
    {
        sum = sum+pushups;
        days++;
    }
    cls();
    std::cout << "In " << days << " days you made " << sum << " Push Ups" << std::endl;

    pushcalc.close();
    float avr = sum/days;

    std::cout << "Your average Push Ups /day is " << avr << std::endl;
}
Last edited on
Can i delete from pushups.txt just the pushups ?
I want to delete just the pushups , and the day to remain...


I don't understand the question. Delete from what?

http://imgur.com/Q3ZTVyi

I want to clear the push ups (12,30,40..) and the days to remain(1,2,3,4...)
Hi I would like to know how can I write this:
Write a program that introduces an array of 10 integers. The program counts the odd elements.
Input: 2, -3, 45, -1, -2, 45, 4, -4, 10, 12 Output 4
@vyangareva - Please do not hijack someone else's thread.
Hi vyangareva, could you start your own new thread for your question instead of piggybacking onto SoLix99's thread?

Why not give the problem a try yourself and post the code you come up with. Think about how you'd figure this out on paper without a computer and maybe that will help frame the code you need to write.
Pages: 1234