0

I already restarted my pc and I even reinstalled Visual Studio, but it does not work. Whenever I start the program, it closes again immediately afterwards.

#include <iostream>
#include <string>
using namespace std;

int main() {
    char c1, c2, c3, c4, c5;
    cout << "Enter five characters: ";
    cin >> c1 >> c2 >> c3 >> c4 >> c5;
    int c1Int = int(c1);
    int c2Int = int(c2);
    int c3Int = int(c3);
    int c4Int = int(c4);
    int c5Int = int(c5);
    string cAllInt = to_string(c1Int) + to_string(c2Int) + to_string(c3Int) + to_string(c4Int) + to_string(c5Int);
    cout << "ASCII Code: " << cAllInt << "." << endl;
    string enteredDelar = "6810110897114";
    if (cAllInt == enteredDelar) {
        cout << "You entered Delar!";
    }
    else {
        cout << "You did not enter Delar." << endl;
        cout << "Because the ASCII code of " << c1 << c2 << c3 << c4 << c5 << " is not 6810110897114." << endl;
    }
    system("pause>0");
}
Delar
  • 1
  • 4
  • Did you try compiling using the terminal and then running it? – HARSH MITTAL May 14 '21 at 19:49
  • @HARSHMITTAL No, I do not know how to do it – Delar May 14 '21 at 19:58
  • What are you trying to do with the system command `pause>0`? That redirects output to a file with the name `0`. If this file cannot be created it will immediately return without running the "pause" command. – 1201ProgramAlarm May 14 '21 at 20:00
  • @1201ProgramAlarm I am learning C++ right now and a Girl on YouTube said that this is what to do when I dont want the console to close when it finished or something like that – Delar May 14 '21 at 20:02
  • $ g++ .cpp -o and to run it use $ – HARSH MITTAL May 14 '21 at 20:04
  • See [this question](https://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) for why you shouldn't use `system("pause");`. – 1201ProgramAlarm May 14 '21 at 20:11
  • Tactical note: When you find yourself with a bunch of sequentially named or numbered variables, odds are good that your program will be easier to write with an array or library container. – user4581301 May 14 '21 at 20:12
  • Recommendation: Always test the results of an IO transaction. If you don't look for errors, every error will be a surprise and lead to increased debugging. Consider replacing `cin >> c1 >> c2 >> c3 >> c4 >> c5;` with `if (cin >> c1 >> c2 >> c3 >> c4 >> c5) { use what was read } else{ handle input error }` – user4581301 May 14 '21 at 20:14
  • 1
    *I already restarted my pc and I even reinstalled Visual Studio* -- So you're using Visual Studio, and you didn't attempt to use the debugger to single-step through the program to see where it fails? (Visual Studio has one of the easiest debuggers to use -- hit F10). You went through all of the trouble of uninstalling Visual Studio, and the cause of the problem could be a simple bug in your program. – PaulMcKenzie May 14 '21 at 20:23
  • I'd suggest you replace that `system("pause>0")` with an `std::getchar()` to wait until you press enter, or a `while (true);` to just pause forever. – mediocrevegetable1 May 14 '21 at 20:24

0 Answers0