-9

I've just started a class with C++, and our first assignment is a simple code that utilizes user supplied variables. I keep getting some syntax errors when I try to compile the code, but according to the book for the class (and other online resources) I think my syntax is correct. Any insight?

EDIT: To clarify, I realize that my syntax is wrong as the compiler is throwing errors, I just was not sure where I was wrong. I appreciate all the help I received.

Code:

//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;

Error(s):

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'
akey
  • 3
  • 2
  • 6
    _" I think my syntax is correct."_ The compiler tells you it's not. – πάντα ῥεῖ Jan 28 '19 at 00:54
  • 2
    Didn't they teach you about `main` function? You can't just write code at file scope. – Igor Tandetnik Jan 28 '19 at 00:56
  • `using namespace std;` <- Bad style. Get a better book, and read more carefully. – Baum mit Augen Jan 28 '19 at 00:58
  • I am taking an online course, and the first chapter in the book (which the assignment is supposed to line up with) only talks about pseudo-code, and how it's important to think in pseudo-code. Do you have any recommendations for an online forum where questions like this would be more "On-Topic"? – akey Jan 29 '19 at 02:17

1 Answers1

2

U made some mistakes here :

  • You have to write your entire main code inside int main()
  • syntax for using cin is cin >> not cin <<
  • You dont need to put char before calavg.str() because it's already declared as stringstream
  • Do not use using namespace std. It is a bad practice that could cause a library conflict Why is "using namespace std" considered bad practice?

Code :

#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;
}
gameon67
  • 3,981
  • 5
  • 35
  • 61
  • Ok, I will edit the post.. Actually using namespace std is easier to be understood by beginner, every programming lecture use that namespace at the beginning (at least for my case) – gameon67 Jan 28 '19 at 01:01
  • 2
    @gameon67 "_Actually using namespace std is easier to be understood by beginner, every programming lecture use that namespace at the beginning_" Debatable. Beating-out the habit of using `using namespace std;`, after initial learning stages is harder, than understanding concept of namespaces (not details, but concept itself). However, most lecturers tend to just not bother. Relevant read: [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Algirdas Preidžius Jan 28 '19 at 01:04
  • Edited. I agree guys. Using namespace std is a very bad habit. I guess I just had a bad programming lecture back in my university – gameon67 Jan 28 '19 at 01:11
  • Wow, thanks y'all! I didn't know that about using namespace std - a lot of online references recommended it, so I thought it was just a convenient little feature. – akey Jan 28 '19 at 02:41
  • @akey For the first month learning programming, it is convenient. But when you are into larger project, it is dangerous. Also kindly accept my answer :) – gameon67 Jan 28 '19 at 02:43
  • Interesting fun fact the amount of bad/harmful C++ reference materials dwarfs the amount of good C++ material. This mean that until the programmer learns enough of the language to be able to tell good from bad, it's much more likely they will pick a bad reference and NEVER learn to tell good from bad. It's really annoying. – user4581301 Jan 28 '19 at 06:15
  • I never knew `namespace std` was bad until I spent more time on SO and read articles on Quora – gameon67 Jan 28 '19 at 06:20
  • I found out the hard way when my reverse function collided with `std::reverse` and the compiler spat out a ball of inscrutable error messages. Great fun. Better than the time I tried to use `strlen` as a variable name in C and found that the `strlen` function was actually a `#define`ed macro. That was a really ugly mess. – user4581301 Jan 28 '19 at 19:09