0

I was testing out Visual studio code for C++, and was writing a madlibs code, but when I compile and execute my code in terminal it outputs something totally bizarre .

I tried different IDEs and they all worked fine except VScode

#include <iostream>

int main()
{
    std::string CharacterName;
    int CharacterAge;
    std::string CharacterNationality;
    std::string CharacterRace;

    std::cout << "Enter your name: ";
    std::cin >> CharacterName;

    std::cout << "Enter your age: ";
    std::cin >> CharacterAge;

    std::cout << "Enter your nationality";
    std::cin >> CharacterNationality;

    std::cout << "Enter your race: ";
    std::cin >> CharacterRace;

    if(CharacterAge > 100) 
   {
        std::cout << "Not valid number";
    }
    else
    {
        std::cout << "Hi! My name is " << CharacterName << ". I am " << CharacterAge << " years old." << "\n";
        std::cout << "I am " << CharacterNationality << " and I am " << CharacterRace << "\n";
    }

    return 0; 
}

Here is the error I'm getting:

error

I expected the code to execute in order of the commands but I misses other commands like inputing other requirements from the user. Here's the result I've been getting:

johnphillip@Johns-MacBook-Pro-2 my_cpp_projects % ./a.out        
Enter your name: 
John Phillip
Enter your age: 
Enter your nationalityEnter your race: 
Hi! My name is John. I am 0 years old.
I am  and I am 
johnphillip@Johns-MacBook-Pro-2 my_cpp_projects % g++ madlibs.cpp
johnphillip@Johns-MacBook-Pro-2 my_cpp_projects % ./a.out        
Enter your name: John Phillip
Enter your age: Enter your nationalityEnter your race: Hi! My name is John. I am 0 years old.
I am  and I am
Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
Mr JPM
  • 81
  • 7

2 Answers2

3

The problem is here

std::cout << "Enter your name: ";
std::cin >> CharacterName;

>> when applied to a string reads a single word. So given your input CharacterName will equal "John". Now you try and read the age but "Phillip" is the next thing to read. Obviously "Phillip" is not a valid age, so your program fails and all subsequent reads fail as well.

Time to read up on how C++ I/O really works, this isn't the only thing that catches newbies out. Don't make assumptions, read the documentation.

john
  • 71,156
  • 4
  • 49
  • 68
1

This answer is nothing more than a complement of @john's answer.


You said "Terminal isn't compiling my code properly".

The terminal is not a compiler, it compiles nothing.
And before saying that the compiler does not compile your code properly, I think you should question your code first of all.

In the current case, the error does not come from the terminal nor the compiler but from your code :)


As @john mentioned, the operator>>() of std::cin (object of class std::istream) reads only single words.

If you want to read strings into std::string objects, I think you should use std::getline() instead.

Example:

std::string characterName;

std::cout << "Enter your name: ";
std::getline(std::cin, characterName);

I hope it can help you.

Fareanor
  • 3,773
  • 1
  • 4
  • 26