skip to Main Content

So I am completely brand new to C++ and learning the basics I watched a YouTube video to help me set up VSC and C++ correctly and initially it was working, I was running programs and the output was showing in the terminal.

However, on this specific program whilst I am trying to implement a while loop, the code simply wont run.
#include

            int main()
            {
                int x = 1;
                int number;

                while(x <= 5){
                    std::cin >> number;
                    x ++;
                }

                return 0;
  PS C:UsersRolla> cd "c:UsersRolla" ; if ($?) { g++ 
  ProgramLoop.cpp -o ProgramLoop } ; if ($?) { .ProgramLoop }. 

Output that it displays

I repeatedly get this error every time I press the run code button.

I have tried to reboot my PC, close VSCode and restart it on task manager and honestly I am such a beginner to C++ and programming as a whole but I can’t see any syntax errors that I done in terms of semi colons or parenthesis etc.

2

Answers


  1. Chosen as BEST ANSWER
            #include <iostream>
    
            int main()
            {
    
                int x = 1;
                int number;
                int total = 0;
    
                while(x <= 5){
                    std::cout << "Please enter a number " << std::endl ;
                    std::cin >> number;
                    total = total + number;
                    x++;
                }
    
                std::cout << "Your total is " << total << std::endl;
    
                return 0;
            }
    

    The part that I was forgetting was the enter a number please.


  2. The code you have written is working perfectly without any issues. The main issue with this code is that it is not structured to achieve any specific output or functionality beyond accepting five integer inputs from the user. In order to see the results in the output/terminal section, you must have to update the code accordingly,

    You can achieve that by adding a small output function into the code, as given in the below example,

    #include <iostream>
    
    int main() {
        int x = 1;
        int number;
    
        while (x <= 5) {
            std::cin >> number;
            std::cout << "Number: " << number << std::endl;
            x++;
        }
    
        return 0;
    }
    
    • std::cout is being used for displaying the value of the variable.
    • std::endl is being used to change the line.

    Output of the code can be,

    Number: 3
    

    For more help, you can refer to the following websites (personal preference)
    C++ reference
    Geeks for Geeks

    I hope it helped you :>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search