2

I am completely new to programming, and I am teaching myself C++ via Dr. Bronson's book.

I have looked at other fixes and I don't understand them.

I am trying to do a project where I convert numbers to their phrase value. 1 = One, 2 = Two, etc. I cannot have spaces between one and two so I am outputting with no spaces. When I opened up the text file, I had only Asian characters in the file. Being a novice, my first concern was that I have been hacked. Anyhow, here is my code.

int main()
{
    int i; 
    int length, mod, number1, number2,number3,numbera,numberb; 
    string  str1;
    string filename = "C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\CPlusPlusProjects\\Project Euler\\Files\\17.WordSum1.txt";
    ofstream outFile; 

    outFile.open(filename.c_str()); 
    if (outFile.fail())
    {
        cout << "\nThe file named " << filename << " did not successfully open."
             << "\Please check to make sure the file exists."; 
    }


    for (i = 900; i <= 999; i++)
    {
        if (i <= 999)
        {
            length = (log(i) / log(10)) + 1;

            cout << "i: " << i << " " << length << endl;
        }

        else if (i == 1000)
        {
            length = 4;
            cout << "i: " << i << " " << length << endl;
        }


        if (length == 3)
        {
            number1 = i % 10; 
            numbera = i / 10; 
            number2 = numbera % 10; 
            numberb = i / 100; 
            number3 = numberb; 
            cout << "Number 3: " << number3 << endl; 
            cout << "Number 2: " << number2 << endl; 
            cout << "Number 1: " << number1 << endl; 
        }


        switch (length)
        {

        case 4: 
        {
        outFile << "OneThousand"; 
        break; 
        }

        case 3: 
        {
            if (number3 == 1)
            {
                outFile << "OneHundredand";
            }
            else if (number3 == 2)
            {
                outFile << "TwoHundredand";
            }
            else if (number3 == 3)
            {
                outFile << "ThreeHundredand";
            }
            else if (number3 == 4)
            {
                outFile << "FourHundredand";
            }
            else if (number3 == 5)
            {
                outFile << "FiveHundredand";
            }
            else if (number3 == 6)
            {
                outFile << "SixHundredand";
            }
            else if (number3 == 7)
            {
                outFile << "SevenHundredand";
            }
            else if (number3 == 8)
            {
                outFile << "EightHundredand";
            }
            else if (number3 == 9);
            {
                outFile << "NineHundredand";
            }
            break;
        }
        }


    }

    return 0; 
}

Text file with Asian characters

Chris Harding
  • 27
  • 1
  • 8
  • As a side note, I think I found a bug in Visual Studio 2017. When I take: Log(1000)/Log(10) I get 2 if all are integers. If all are double, I get 3.0. If I try to add 2 to the int value of 2, I get 5. I reported that to Microsoft. – Chris Harding Jun 05 '17 at 02:32
  • There is no text but encoded text. When you write a file, you chose the character encoding. Readers just have to use it, which means you have to tell them. It all begins with your source code, which, as text, has an encoding that you have your editor use. You then have to tell your compiler what the "[source character set](https://learn.microsoft.com/en-us/cpp/build/reference/source-charset-set-source-character-set)" is. (In all likelihood, defaults for these match up without you knowing it.) Repeat each time text is written and then read. Hint: /execution-charset is next. – Tom Blodget Jun 05 '17 at 16:52

2 Answers2

0

It looks like you've found a corner case where Notepad's encoding detection guesses wrong.

The string "NineHundredand", encoded using ASCII gives the byte sequence 0x4E 0x69 0x6E 0x48 0x75 0x6E 0x64 0x72 0x65 0x64 0x61 0x6E 0x64. It turns out that same sequence of bytes is also valid UTF-16 encoding for the string "楎敮畈摮敲慤摮". Since there's nothing to tell Notepad which interpretation of the bytes is correct, it has to guess. How it makes the guess it does is likely quite complicated and probably not documented anywhere (at least not publicly), but in this case it guessed wrong. Strangely enouough, it seems to depend on how many times you repeat the phrase "NineHundredand". Less than 15 times and it will guess the encoding as ASCII; more than that and it decides the file is encoded using UTF-16.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • Ha! Only I am supposed to guess. Anyhow, I am not sure how I can get it to make the right decision. Earlier, I got it to output the right format but that was with a different switch statement. In that case, although I have else if (i == 9), nine would be reproduced after every number. That is a different story. – Chris Harding Jun 05 '17 at 03:00
  • I've only been able to get it to guess wrong by repeating the strings `"Nine"` or `"NineHundredand"` with no line breaks or spaces. This really does seem to be a corner case. – Miles Budnek Jun 05 '17 at 03:02
  • I also tried the output with a space between the words and got Asian characters as well. – Chris Harding Jun 05 '17 at 03:07
  • Yeah, I have NO idea why "Nine Hundred and" is repeating, either. Text File Quote: "One Hundred andNine Hundred and" – Chris Harding Jun 05 '17 at 03:12
  • `"NineHundredand"` is repeating because all of the numbers between `900` and `999` begin with a nine and you never print anything for any digit besides the hundreds digit. – Miles Budnek Jun 05 '17 at 03:14
  • I changed it to the 100 to 199 range before I got the results I posted. – Chris Harding Jun 05 '17 at 03:17
  • Oh, there's an errant semicolon after your `else if (number3 == 9)` – Miles Budnek Jun 05 '17 at 03:22
  • Yeah, I should have caught that one. Thanks for the catch. I get English phrases for 100-300; 600-800. I get Asian characters for 400-500; 900. – Chris Harding Jun 05 '17 at 03:38
-1

I complie your code by gcc in linux, there are some errors.

as.cpp: In function ‘int main()’:
as.cpp:9: error: aggregate ‘std::ofstream outFile’ has incomplete type and cannot be defined
as.cpp:15: error: unknown escape sequence '\P'
as.cpp:23: error: ‘log’ was not declared in this scope

And I add math.h to correct log problem.Changing \P to \nP to correct the second one.As to the first one correct by using fstream.My code is on the below.(only provide first 17 lines,others are equals to yours)

1 #include<iostream>
  2 #include<fstream>
  3 #include<math.h>
  4 using namespace std;
  5 int main()
  6 {
  7         int i;
  8         int length, mod, number1, number2,number3,numbera,numberb;
  9         string  str1;
 10         string filename = "C:\\Users\\miram\\OneDrive\\Documents\\Visual Studio 2017\\CPlus    PlusProjects\\Project Euler\\Files\\17.WordSum1.txt";
 11         std::ofstream outFile;
 12         outFile.open(filename.c_str());
 13         if (outFile.fail())
 14         {
 15                 cout << "\nThe file named " << filename << " did not successfully open."
 16                         << "\nPlease check to make sure the file exists.";
 17         }

And here is part of outputs.

  1 i: 900 3
  2 Number 3: 9
  3 Number 2: 0
  4 Number 1: 0
  5 i: 901 3
  6 Number 3: 9
  7 Number 2: 0
  8 Number 1: 1
  9 i: 902 3
 10 Number 3: 9
 11 Number 2: 0
 12 Number 1: 2
 13 i: 903 3
 14 Number 3: 9
 15 Number 2: 0
 16 Number 1: 3
 17 i: 904 3
 18 Number 3: 9
 19 Number 2: 0
 20 Number 1: 4
 21 i: 905 3
 22 Number 3: 9
 23 Number 2: 0
 24 Number 1: 5
 25 i: 906 3
 26 Number 3: 9
 27 Number 2: 0
 28 Number 1: 6
 29 i: 907 3
 30 Number 3: 9
 31 Number 2: 0
 32 Number 1: 7
potatoes
  • 3
  • 3