我刚刚用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'
答案 0 :(得分:2)
U在这里犯了一些错误:
int main()
内编写整个主要代码cin >>
而不是cin <<
char
放在calavg.str()
之前,因为它已经被声明为stringstream using namespace std
。这是一种不良做法,可能会导致库冲突Why is "using namespace std" considered bad practice? 代码:
#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;
}