用C++编译问题 我刚刚开始用C++编写一个类,我们的第一个任务是一个简单的代码,它使用用户提供的变量。当我试图编译代码时,我总是会遇到一些语法错误,但是根据该类的书和其他在线资源,我认为我的语法是正确的。有什么见解吗

用C++编译问题 我刚刚开始用C++编写一个类,我们的第一个任务是一个简单的代码,它使用用户提供的变量。当我试图编译代码时,我总是会遇到一些语法错误,但是根据该类的书和其他在线资源,我认为我的语法是正确的。有什么见解吗,c++,syntax,C++,Syntax,编辑:为了澄清,我意识到我的语法是错误的,因为编译器正在抛出错误,我只是不确定我错在哪里。我感谢所有的帮助 代码: 错误: Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27026.1 for x86 Copyright (C) Microsoft Corporation. All rights reserved. SP2019_Lab1Part2_Key.cpp SP2019_Lab1Part2_Key.cpp(9): erro

编辑:为了澄清,我意识到我的语法是错误的,因为编译器正在抛出错误,我只是不确定我错在哪里。我感谢所有的帮助

代码:

错误:

Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27026.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

SP2019_Lab1Part2_Key.cpp
SP2019_Lab1Part2_Key.cpp(9): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(10): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(10): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(14): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(14): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(16): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(21): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(21): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(23): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(23): error C2086: 'int cin': redefinition
SP2019_Lab1Part2_Key.cpp(16): note: see declaration of 'cin'
SP2019_Lab1Part2_Key.cpp(28): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(28): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(30): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(30): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(30): error C2086: 'int cin': redefinition
SP2019_Lab1Part2_Key.cpp(16): note: see declaration of 'cin'
SP2019_Lab1Part2_Key.cpp(40): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(40): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(40): error C2371: 'calavg': redefinition; different basic types
SP2019_Lab1Part2_Key.cpp(39): note: see declaration of 'calavg'
SP2019_Lab1Part2_Key.cpp(41): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(41): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(41): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(42): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(42): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(42): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'

你在这里犯了一些错误:

您必须在int main中编写整个主代码
使用cin的语法是cin>>不是cin我认为我的语法是正确的。编译器告诉你不是。他们没有教你主函数吗?您不能只在文件范围内编写代码;我正在学习一门在线课程,本书的第一章(作业应该与之对齐)只讨论伪代码,以及用伪代码思考的重要性。你对在线论坛有什么建议吗?像这样的问题会更多地集中在主题上?好的,我会编辑这篇文章。。实际上,使用名称空间std对初学者来说更容易理解,每节编程课在开始时都会使用该名称空间,至少对我来说是这样case@gameon67实际上,初学者更容易理解使用名称空间std,每次编程讲座一开始都会使用该名称空间。打破使用名称空间std的习惯;,在最初的学习阶段之后,要比理解名称空间的概念更难——不是细节,而是概念本身。然而,大多数讲师倾向于不费吹灰之力。相关阅读:已编辑。我同意,伙计们。使用名称空间std是一个非常坏的习惯。我想我刚在大学里上了一堂糟糕的编程课。噢,谢谢大家!我不知道如何使用namespace std-很多在线参考都推荐它,所以我认为它只是一个方便的小功能。@akey在学习编程的第一个月,它很方便。但当你进入一个更大的项目时,这是危险的。也请接受我的回答:
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27026.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

SP2019_Lab1Part2_Key.cpp
SP2019_Lab1Part2_Key.cpp(9): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(10): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(10): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(14): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(14): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(16): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(21): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(21): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(23): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(23): error C2086: 'int cin': redefinition
SP2019_Lab1Part2_Key.cpp(16): note: see declaration of 'cin'
SP2019_Lab1Part2_Key.cpp(28): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(28): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(30): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(30): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(30): error C2086: 'int cin': redefinition
SP2019_Lab1Part2_Key.cpp(16): note: see declaration of 'cin'
SP2019_Lab1Part2_Key.cpp(40): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(40): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(40): error C2371: 'calavg': redefinition; different basic types
SP2019_Lab1Part2_Key.cpp(39): note: see declaration of 'calavg'
SP2019_Lab1Part2_Key.cpp(41): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(41): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(41): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(42): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(42): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(42): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::cout << "SP2019_Aaron Key - OUTPUT OF USING VARIABLES" << std::endl;
    //declares string variable "Word"
    char Word;
    //Prompts user for word:
    std::cout << "Word: ";
    //Waits for input for value of "Word" from standard input (keyboard)
    std::cin >> Word;

    //Declares integer variable "FirstNumber"
    double FirstNumber;
    //Prompts user for a number:
    std::cout << "First Number: ";
    //Waits for number:
    std::cin >> FirstNumber;

    //Declares double variable "SecondNumber":
    double SecondNumber;
    //Prompts user for a number:
    std::cout << "Second Number: ";
    //Waits for input:
    std::cin >> SecondNumber;

    //Adds FirstNumber and SecondNumber:
    double SUM = FirstNumber + SecondNumber;
    //Calculates Average of FirstNumber and SecondNumber:
    double Avg = SUM / 2;


    //Gives the average of FirstNumber and SecondNumber:
    std::stringstream calavg;
    calavg << "The average of " << FirstNumber << " and " << SecondNumber 
    << " is: " << Avg;
    std::cout <<  calavg.str();

    return 0;
}