用C++编译问题

Compiling Issue with C++

本文关键字:问题 编译 C++      更新时间:2023-10-16

我刚刚用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;
}