File I/O problem.

May 28, 2009 at 9:39pm
I'm looking to get numbers from a file, which has them laid out like this:

1
2
3
4
5

How can I do this? Bearing in mind the numbers will need to be added up later. Also, they might change at some point.

Could I do this:
1
2
3
4
5
6
7
8
9
10
 int num1, num2, num3;

ifstream file("file.txt");


file >> num1;
file >> num2;
file >> num3:

int num4=num1+num2+num3;
?

If not, how could I do it?

I'm trying to get each line in sequence, based on input aswell.


The actual program is a bit silly. I was reading the news one day, and they said something about a virtual judging system being used in the UK. I thought it would fun to try 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
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
129
130
131
132
133
// I shall add the possibility of a Not Guilty plea soon; but then I'll have to
// write the jury function too, it will be complicated.

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <windows.h>
#define SENTENCE PlaySound(TEXT("Voice/sent.wav"  ), NULL, SND_FILENAME);
#define GAVEL    PlaySound(TEXT("Voice/gavel.wav" ), NULL, SND_FILENAME);
#define YEARS    PlaySound(TEXT("Voice/years.wav" ), NULL, SND_FILENAME);
#define PRISON   PlaySound(TEXT("Voice/prison.wav"), NULL, SND_FILENAME);

using namespace std;

void    guilty(string, string, string);
/*
void notguilty(string, string, string);
void      jury(string, string, string);
*/
int main() {
string charge1, charge2, charge3, yesno, plead;

cout << "Please input all data UNCAPTIALIZED.\n\n";

do {
    cout << "Input charge 1: ";
    cin >> charge1;
    cin.ignore();
    cout << "\nAny further charges?[Y/N]\n\n";
    cin >> yesno;
    cin.ignore();

    if (yesno=="n") {charge2==""; charge3==""; break;}
    
    cout << "\nInput charge 2: ";
    cin >> charge2;
    cin.ignore();
    cout << "\nAny further charges?[Y/N]\n\n";
    cin >> yesno;
    cin.ignore();

    if (yesno=="n") {charge3==""; break;}

    cout << "\nInput charge 3: ";
    cin >> charge3;
    cin.ignore();
    break;
} while (true);
/*
do {
    cout << "\nInput plead: [\"Guilty\"/\"Not Guilty\"]\n";
    cin >> plead;
    cin.ignore();
    break;
} while (true);

if (plead=="guilty") 
     guilty(charge1, charge2, charge3);
else 
  notguilty(charge1, charge2, charge3);
*/

guilty(charge1, charge2, charge3);
YEARS
PRISON
GAVEL
cin.get();
return 0;
}

void    guilty(string charge1, string charge2, string charge3) {
int sentence=0;
if (charge1!="" && charge2=="" && charge3=="")
    cout << "\n\nYou have pleaded guilty to: " << charge1 << endl;
if (charge1!="" && charge2!="" && charge3=="")
    cout << "\n\nYou have pleaded guilty to: " << charge1 << " and " << charge2 << endl;
if (charge1!="" && charge2!="" && charge3!="")
    cout << "\n\nYou have pleaded guilty to: " << charge1 << ", " << charge2 << " and " << charge3 << endl;

Sleep(3000);
system("cls");
cout << "Judging.";
Sleep(1000);
system("cls");
cout << "Judging..";
Sleep(1000);
system("cls");
cout << "Judging...";
system("cls");

int num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0;

ifstream file("file.txt");

if (charge1=="murder" || charge2=="murder" || charge3=="murder")
    file >> num1;
if (charge1=="manslaughter" || charge2=="manslaughter" || charge3=="manslaughter")
    file >> num2;
if (charge1=="rape" || charge2=="rape" || charge3=="rape")
    file >> num3;
if (charge1=="assault" || charge2=="assault" || charge3=="assault")
    file >> num4;
if (charge1=="theft" || charge2=="theft" || charge3=="theft")
    file >> num5;
if (charge1=="subabu" || charge2=="subabu" || charge3=="subabu")
    file >> num6;
if (charge1=="hitrun" || charge2=="hitrun" || charge3=="hitrun")
    file >> num7;

sentence=(num1+num2+num3+num4+num5+num6+num7);

cout << "Decision found.";

Sleep(3000);
system("cls");
cout << "Sentencing.";
Sleep(800);
system("cls");
cout << "Sentencing..";
Sleep(800);
system("cls");
cout << "Sentencing...";
system("cls");
Sleep(500);
SENTENCE
cout << sentence;
Sleep(500);
}

/*
void notguilty(string charge1, string charge2, string charge3) {

}*/


That's the actual code. This is the part I need help with though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0;

ifstream file("file.txt");

if (charge1=="murder" || charge2=="murder" || charge3=="murder")
    file >> num1;
if (charge1=="manslaughter" || charge2=="manslaughter" || charge3=="manslaughter")
    file >> num2;
if (charge1=="rape" || charge2=="rape" || charge3=="rape")
    file >> num3;
if (charge1=="assault" || charge2=="assault" || charge3=="assault")
    file >> num4;
if (charge1=="theft" || charge2=="theft" || charge3=="theft")
    file >> num5;
if (charge1=="subabu" || charge2=="subabu" || charge3=="subabu")
    file >> num6;
if (charge1=="hitrun" || charge2=="hitrun" || charge3=="hitrun")
    file >> num7;

sentence=(num1+num2+num3+num4+num5+num6+num7);
Last edited on May 28, 2009 at 9:49pm
May 28, 2009 at 11:43pm
1
2
3
4
5
6
7
8
9
10
 int num1, num2, num3;

ifstream file("file.txt");


file >> num1;
file >> num2;
file >> num3:

int num4=num1+num2+num3;

Cant do this, There are sooooo many post about this topic its stupid, search

[code]
May 29, 2009 at 3:53am
Hey, how about some actual help?

It looks to me like your file is a list of the number of years each type of crime is worth in sentencing, something like:

25 years for murder
15 years for manslaughter
etc

And you want to add in only those charges wherewith the accused stands...

You will need to read all the numbers from file, then only add those that apply:
1
2
3
4
5
6
7
  file >> years_for_murder
       >> years_for_manslaughter
       >> years_for_rape
       >> years_for_assault
       >> years_for_theft
       >> years_for_subabu
       >> years_for_hitrun;
Then only add in those values for which the charges apply:
1
2
3
4
5
6
  string all_charges = charge1 +charge2 +charge3;
  int sentence = 0;

  if (all_charges.find( "murder"       ) != string::npos) sentence += years_for_murder;
  if (all_charges.find( "manslaughter" ) != string::npos) sentence += years_for_manslaughter;
  ...

Hope this helps.
Last edited on May 29, 2009 at 3:54am
May 29, 2009 at 3:26pm
Thanks Duoas.

Yes, it works. Thanks.

There is still a problem though;
This is the code
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
// Open charges.txt <-- this file contains the charges for each crime.
ifstream charges("charges.txt");
// Read from the file
charges >> sentence_murder
        >> sentence_manslaughter
        >> sentence_rape
        >> sentence_assault
        >> sentence_theft
        >> sentence_subabu
        >> sentence_hitrun;
        
string totalsentence=(charge1+charge2+charge3);

if (totalsentence.find("murder"      )!=string::npos) sentence+=sentence_murder;
if (totalsentence.find("manslaughter")!=string::npos) sentence+=sentence_manslaughter;
if (totalsentence.find("rape"        )!=string::npos) sentence+=sentence_rape;
if (totalsentence.find("assault"     )!=string::npos) sentence+=sentence_assault;
if (totalsentence.find("theft"       )!=string::npos) sentence+=sentence_theft;
if (totalsentence.find("subabu"      )!=string::npos) sentence+=sentence_subabu;
if (totalsentence.find("hitrun"      )!=string::npos) sentence+=sentence_hitrun;
if (totalsentence.find("murder"      )!=string::npos) sentence+=sentence_murder;
if (totalsentence.find("manslaughter")!=string::npos) sentence+=sentence_manslaughter;
if (totalsentence.find("rape"        )!=string::npos) sentence+=sentence_rape;
if (totalsentence.find("assault"     )!=string::npos) sentence+=sentence_assault;
if (totalsentence.find("theft"       )!=string::npos) sentence+=sentence_theft;
if (totalsentence.find("subabu"      )!=string::npos) sentence+=sentence_subabu;
if (totalsentence.find("hitrun"      )!=string::npos) sentence+=sentence_hitrun;


Even if I type in assault, they get the charge for murder.

This is the text file:
1
2
3
4
5
6
7
30 years for murder
25 years for manslaughter
10 years for rape
15 years for assault
10 years for subabu
10 years for hitrun
5   years for theft


No matter what I enter, they get 30 years. Even if there is only one charge, and it is theft.
Last edited on May 29, 2009 at 3:48pm
May 29, 2009 at 6:04pm
It is because you can't use
file >> int_var;
with non-integer values, like "years" and "for".

Your input file should look like this:
1
2
3
4
5
6
7
30
25
10
15
10
10
5

Otherwise you'll have to modify your input statements to account for the extra stuff on each line.
May 29, 2009 at 6:26pm
Alternatively, if you want to leave the extra text in the input file, you could do this:

1
2
3
charges >> sentence_murder;       charges.ignore(99, '\n');
charges >> sentence_manslaughter; charges.ignore(99, '\n');
...
May 29, 2009 at 6:42pm
Or this ;-]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <limits>

template <typename CharType, typename CharTraits>
std::basic_istream <CharType, CharTraits> &
endl(
  std::basic_istream <CharType, CharTraits> & ins
  ) {
  return ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  }

...

charges >> sentence_murder       >> endl
        >> sentence_manslaughter >> endl;

XD
Last edited on May 29, 2009 at 6:44pm
May 29, 2009 at 7:16pm
Thanks guys. It now works.

=]
Jun 1, 2009 at 12:25am
@Duoas
speaking of help, What the HELL was this LOL
-> http://cplusplus.com/forum/general/11369/
Jun 1, 2009 at 1:32am
@jloundy
If all you want to do is help cheaters and tell people actually seeking real help nothing more than "stupid, search"... then I can live with that without giving you too much grief.

But if you want to start following me around for a fight and to start spamming people's posts with links to unrelated stuff, I'll take it to the administrators. BACK OFF.
Jun 1, 2009 at 3:17pm
dang to get all fiesty lol its the begineer form which means almost everyone see's eachothers posts. I was Just wondering what that post was all about, PLEASE DONT TELL ON ME :( bahahahah
Last edited on Jun 1, 2009 at 3:18pm
Jun 1, 2009 at 9:22pm
LOL, sorry. See the other thread for more.
Topic archived. No new replies allowed.