Hi,

I need to do the following:

  1. read the text file with names and surnames
  2. convert each char to int than add them
  3. the number must keep adding each other until it is under 10
This is what I have so far
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main ()
{
  char charValue1[1000], charValue2[1000], in_line[15], out_line[15];
  int asciiValue, charNum, word = 0;
  string line;

  //input and output files
  ifstream in_stream;
  ofstream out_stream;

  cout << "Enter the file name of the input file (max char 14): " << endl;
  cin >> in_line;
  cout << in_line << endl;

  cout << "Enter the file name of the output file (max char 14): " << endl;
  cin >> out_line;
  cout << out_line << endl;

  //open the  file
in_stream.open(in_line);
//Check if the file can be opened
if (in_stream.fail())
  {
    cout << "Input file error" << endl;
    exit(1);
  }

out_stream.open(out_line);


//Read the file till EOF

while (!in_stream.eof())
   {

      {

         while (getline(in_stream, line))
         {
           cout << int(charValue1[line]) << endl;
           cout << charValue1[line] << endl;
           word++;
         }
      }

   }


in_stream.close();
out_stream.close();

    return 0;
}

========================================================================

the file will contain e.g

"worda wordb; wordc wordd; worde wordf;"

for example the program needs to convert the worda and wordb into Ascii numbers and add them e.g
(w+o+r+d+a) + (w+o+r+d+b) --> keep adding them until the number is less than 10
for example 1.1

w = 1, o = 2, r = 3, d = 4, a = 5, b = 6
(w+o+r+d+a) + (w+o+r+d+b)
= (1+2+3+4+5) + (1+2+3+4+6)
= 15 + 16
= 31
= 4 ( less than 10)

which will be written into the output file.

The words will be separated by ";"

I'm stuck on how to separate the 2 words by ";" and also how to get the 2nd two words after ";" than stop at ";" etc...

Recommended Answers

All 4 Replies

You should use code tags!

OK, you can use getline() for this:

std::ifstream infile("myFilename.txt", std::ios::in);
std::string word1, word2;
while(infile.fail() != true){
    getline(infile, word1, ';');
    getline(infile, word2, ';');

    /* Now do things with that you want with the words */
}

This will get words from the file two at a time, you can then write the code to do the maths that you want to do on each word and put it inside the while loop. You can read more about getline() here.

ok thank... so I got that part right...
but now I cant convert string into char
=====================================================================

while(in_stream.fail() != true)
         {
           getline(in_stream, stringValue1, ';');
           getline(in_stream, stringValue2, ';');
           cout << stringValue1 << endl;
           cout << stringValue2 << endl;
             charValue1[] = stringValue1;
           while (charValue1[word] != '\0')
              {
                 cout << int(charValue1[word]) << endl;
                   //cout << charValue1[word] << endl;
                 word++;
              }

=====================================================================

trying to get the logic right with the following.
1. convert string to char
2. convert each char to ascii
3. add word1
4. add word2
5. keep adding word1 and word2 until < 10

so do I keep using the while loop in while loop... I'm sure there is a better way... I just started C++ (new to programming) so I dont know all the functions.

ok thank... so I got that part right...
but now I cant convert string into char

You don't need to.
stringValue1[0] is the first character.
stringValue1[1] is the second character.
stringValue1[4] is the fifth character.

You don't need to put the string into a character array.

You can use the characters of stringValue1 directly (line 7 doesn't work, which you probably found out already). stringValue1[word] gives the proper character, and stringValue1.size() gives you the length.

I just started C++ (new to programming) so I dont know all the functions.

Your professor knows that, which is why she/he probably wants you to do it the long way.

There are a couple of ways that you can do the next step, either way you have to get the digits from the numeric result you find. See what you can come up with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.