用C++编译问题
Compiling Issue with C++
我刚刚用C++开始了一个类,我们的第一个赋值是一个使用用户提供的变量的简单代码。当我试图编译代码时,我总是会遇到一些语法错误,但根据该类的书(以及其他在线资源),我认为我的语法是正确的。有什么见解吗?
编辑:为了澄清,我意识到我的语法是错误的,因为编译器正在抛出错误,我只是不确定我错在哪里。我感谢大家对我的帮助。
代码:
//SP2019_Lab1Part2_Key.cpp
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
cout << string(50, '-') << endl;
cout << "SP2019_Aaron Key - OUTPUT OF USING VARIABLES" << endl;
//declares string variable "Word"
char Word;
//Prompts user for word:
cout << "Word: ";
//Waits for input for value of "Word" from standard input (keyboard)
cin << Word;
//Declares integer variable "FirstNumber"
double FirstNumber;
//Prompts user for a number:
cout << "First Number: ";
//Waits for number:
cin << FirstNumber;
//Declares double variable "SecondNumber":
double SecondNumber;
//Prompts user for a number:
cout << "Second Number: ";
//Waits for input:
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:
stringstream calavg;
calavg << "The average of " << FirstNumber << " and " << SecondNumber
<< " is: " << Avg;
cout << char calavg.str();
cout << string(50, '-') << endl;
错误:
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'
U在这里犯了一些错误:
- 您必须在
int main()
中编写整个主代码 - 使用cin的语法是
cin >>
而不是cin <<
- 您不需要将
char
放在calavg.str()
之前,因为它已经声明为字符串流 - 不要使用
using namespace std
。这是一种可能导致图书馆冲突的不良做法;使用命名空间std";被认为是不好的做法
代码:
#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;
}
相关文章:
- 编译包含字符串的代码时遇到问题
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 编译要在英特尔Hyperscan中使用的.cc文件时出现问题
- 如何在 Mac 上正确编译C++,当它在 Linux 上编译没有问题时?
- Rextester 语言编译C++问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 编译 llvm 插件时出现问题:llvm/Config/llvm-config.h:没有这样的文件或目录
- C++ 多级虚拟继承编译问题
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- Qt Visual Studio 2015 加载项编译问题
- 构造函数和 G++ 编译配方的问题
- 解析问题 - 预期的非限定 ID - #include <array> 编译错误
- libssh 问题编译问题未定义的错误
- Eclipse 问题 - 编译期间不考虑 .c 和 .cpp 文件中定义的预处理
- Visual Studio 2017问题编译了C 代码,而该代码则在Linux中编译
- 这个C++代码有什么问题?编译错误:调用“测试::测试(测试)”没有匹配函数
- 试图编译基本Boost PropertyTree示例时出现问题-编译错误.该怎么做
- 如何解决***缺少分隔符的问题.编译QT(或支持库)时