Error occur in programming C++ language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Clement Lo
    New Member
    • Apr 2011
    • 2

    Error occur in programming C++ language

    I am beginner in C++ language programming and using Microsoft Visual 2010.

    The simple code I wrote myself as follow:
    Code:
    // Hello world.cpp : main project file.
    
    #include "stdafx.h"
    
    #include "iostream"
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
        //define variables
    	float num1;
    	float num2;
    	float total;
    
    	//enter data for variables
        std::cout << "Enter a value for a first variable: ";
    	std::cin >> num1;
    	std::cout << "Enter a value for a second variable: ";
    	std::cin >> num2;
    
    	//add two numbers together
    	total= num1 + num2;
    
    	//display the results
    	cout << "The sum of the variables =" <<total << endl;
    }

    I got error message as below. Can anyone help me?

    1>------ Build started: Project: Hello world, Configuration: Debug Win32 ------
    1> Hello world.cpp
    1>Hello world.cpp(26): error C2065: 'cout' : undeclared identifier
    1>Hello world.cpp(26): error C2065: 'endl' : undeclared identifier
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by Banfa; Apr 27 '11, 08:20 AM. Reason: Added [code]...[/code] tags round the code, please use them in future.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
    //display the results
    cout << "The sum of the variables =" <<total << endl;
    You forgot the std:: in this line of code.

    Comment

    • Clement Lo
      New Member
      • Apr 2011
      • 2

      #3
      I added std:: But there is another error message popped up.

      Code:
      #include <iostream>
      using namespace std;
      
      #include <math.h>
      
      int main(void)
      {
      	//define variables
      	float num1;
      	float num2;
      
      	//enter data for the variables
      	std::cout << "Enter a value for the first variable: ";
      	std::cin >> num1;
      	std::cout << "Enter a value for the second variable: ";
      	std::cin >> num2;
      
      	//Add the two number together
      	std::equal = num1 + num2;
      
      	//Display the result
      	std::cout << "The sum of the number=" << total;
      }
      1>------ Build started: Project: First project, Configuration: Debug Win32 ------
      1> main.cpp
      1>c:\users\jhu n yan lo\documents\vi sual studio 2010\projects\f irst project\first project\main.cp p(19): error C2659: '=' : function as left operand
      1>c:\users\jhu n yan lo\documents\vi sual studio 2010\projects\f irst project\first project\main.cp p(22): error C2065: 'total' : undeclared identifier
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

      Please advise.

      Thank you.
      Last edited by Banfa; Apr 27 '11, 08:20 AM. Reason: Added [code]...[/code] tags round the code, please use them in future.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        //Add the two number together
        std::equal = num1 + num2;
        
        //Display the result
        std::cout << "The sum of the number=" << total;
        What is std::equal?

        And where do your define "total"?

        Comment

        Working...