Home c++ There was a problem with reading numbers into an array via Fstream...

There was a problem with reading numbers into an array via Fstream on C++

Author

Date

Category

You need to implement reading numbers from the file and the subsequent entry into an array.
Here is the code:

# include "stdafx.h"
#Include & lt; iostream & gt;
#Include & lt; fstream & gt;
#Include & lt; String & GT;
Using Namespace STD;
INT MAIN ()
{
  // Phase announcement
  SETLOCALE (LC_ALL, "RUS");
  String IMENA [] ​​= {"Camille Omarov -", "Ivan Ryzhov -", "Yaroslav Protasov -", "Dryo Corvius -", ​​"Victor Romanov -", "Dmitry Voronov -", "Ivan Strelletsky -", "Danil Khalimov - "," Rustam Gabbasov - "," Mikhail Timofeev - "};
  INT PLUS [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  STRING VVOD [10] = {"0 -", "1 -", "2 -", "3 -", "4 -", "5 -", "6 -", "7 -", "8 - ", "nine - ", };
  int pvvod = 0;
  int x = 0;
  Char Vopros [] = "Press '1' if you want to give it plus.";
  Char Menu [] = "Press '2' to return to the menu.";
  Char Vid [] = "Click '1' and we give it plus (we are gods)";
  // Phase reading
  // Phase show
Menu:
  COUT & LT; & LT; vvod [0] & lt; & lt; IMENA [0] & lt; & lt; Plus [0] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [1] & lt; & lt; IMENA [1] & lt; & lt; Plus [1] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [2] & lt; & lt; IMENA [2] & lt; & lt; Plus [2] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [3] & lt; & lt; IMENA [3] & lt; & lt; Plus [3] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [4] & lt; & lt; IMENA [4] & lt; & lt; Plus [4] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [5] & lt; & lt; IMENA [5] & lt; & lt; Plus [5] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [6] & lt; & lt; IMENA [6] & lt; & lt; Plus [6] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [7] & lt; & lt; IMENA [7] & lt; & lt; Plus [7] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [8] & lt; & lt; IMENA [8] & lt; & lt; Plus [8] & lt; & lt; Endl;
  COUT & LT; & LT; vvod [9] & lt; & lt; IMENA [9] & lt; & lt; Plus [9] & lt; & lt; Endl;
  // Phase receiving input
  CIN & GT; & GT; x;
  // Phase reading / issuing information
  ifstream fin;
  Fin.open ("gym.txt");
  FIN.Close ();
  // Phase calculation
  COUT & LT; & LT; Vopros & lt; & lt; Endl;
  COUT & LT; & LT; MENU & LT; & LT; Endl;
  CIN & GT; & GT; pvvod;
  if (pvvod == 1)
  {
    COUT & LT; & LT; IMENA [x] & lt; & lt; Plus [x] & lt; & lt; Endl;
    if (pvvod == 1)
    {
      Plus [x] ++;
      COUT & LT; & LT; "We gave him a plus ... Go, traveler. \ N";
      COUT & LT; & LT; "\ n";
      Ofstream Fout ("gym.txt");
      FOUT & LT; & LT; IMENA [0] & lt; & lt; Plus [0] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [1] & lt; & lt; Plus [1] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [2] & lt; & lt; Plus [2] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [3] & lt; & lt; Plus [3] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [4] & lt; & lt; Plus [4] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [5] & lt; & lt; Plus [5] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [6] & lt; & lt; Plus [6] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [7] & lt; & lt; Plus [7] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [8] & lt; & lt; Plus [8] & lt; & lt; Endl;
      FOUT & LT; & LT; IMENA [9] & lt; & lt; Plus [9] & lt; & lt; Endl;
      Fout.Close ();
      Goto Menu;
    }
  }
  ELSE.
  {
    Goto Menu;
  }
}

Here is an example of data in the file:

Camille Omarov -1
Ivan Ryzhov -1.
Yaroslav Protasov -1.
Dryo Corvius -0.
Victor Romanov -0.
Dmitry Voronov -0.
Ivan Strelletsky -0.
Danil Khalimov -0.
Rustam Gabbasov -0.
Mikhail Timofeev -0.

Answer 1

Here is an example code with comments:

ifstream fin;
Fin.open ("gym.txt");
String T;
INT COUNT = 0;
int i = 0, number = 0;
// Read the string to the first '\ n' that is a string
While (GetLine (FIN, T, '\ N')) {
  i = t.size () - 1;
  // Starting from the end of the line, we go to a symbol that stands before the number and
  // is not part of this number
  While (I & GT; = 0 Δ & amp; t [i] & gt; = '0' & amp; & amp; t [i] & lt; = '9') --i;
  // write the first digit
  Number = T [++ i] - '0';
  ++ I;
  // and then the usual parser from the string
  While (i & lt; t.size () & amp; & amp; t [i] & gt; = '' '& amp; & amp; t [i] & lt; =' 9 ') {
    Number = Number * 10 + (t [i] - '0');
    ++ I;
  } 
// write to the plus array received number
   PLUS [COUNT ++] = NUMBER;
}
FIN.Close ();

Here is a way much easier (for some reason I didn’t think right away):

ifstream fin;
Fin.open ("gym.txt");
String T;
INT COUNT = 0;
int i = 0;
// Read the string to the first '\ n' that is a string
While (GetLine (FIN, T, '\ N')) {
   i = t.size () - 1;
   // Starting from the end of the line, we go to a symbol that stands before the number and
   // is not part of this number
   While (I & GT; = 0 Δ & amp; t [i] & gt; = '0' & amp; & amp; t [i] & lt; = '9') --i;
   // Using the ATOI function to resign a row to the number
   PLUS [COUNT ++] = ATOI (T.C_STR () + I + 1);
}
FIN.Close ();

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions