Question

In: Computer Science

Whenever I am attempting to write a simple program on C++ I get an error message...

Whenever I am attempting to write a simple program on C++ I get an error message that reads "cout was not declared in this scope". Literally every time. This has become frustrating because I have even written my code the exact same way as some of my classmates who got theirs to compile and run with no sign of this error at all, and yet min gives this answer. I will leave an example of a code where this error message appeared. Hopefully you can help. Thank you.

ex:

// Example program
#include <iostream>
#include <string>

int main()
{
double mass, velocity,energy;
cout<<"\n enter the objects weight";
cin>>mass;
cout<<"\n enter the objects velocity"
cin>>velocity;
energy=(1.0/2.0)*(mass*velocity)*2;
cout<<"\n mass = "<<mass<<"kg";
cout<<"\n velocity = "<<velocity<<"m/s";
cout<<"\n kinetic energy = "<<energy<<"kgm/s";
}

Here's the error message:

In function 'int main()': 8:1: error: 'cout' was not declared in this scope 8:1: note: suggested alternative: In file included from 2:0: /usr/include/c++/4.9/iostream:61:18: note: 'std::cout' extern ostream cout; /// Linked to standard output ^ 9:1: error: 'cin' was not declared in this scope 9:1: note: suggested alternative: In file included from 2:0: /usr/include/c++/4.9/iostream:60:18: note: 'std::cin' extern istream cin; /// Linked to standard input ^

Solutions

Expert Solution

Code:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
double mass, velocity,energy;
cout<<"\n enter the objects weight";
cin>>mass;
cout<<"\n enter the objects velocity";
cin>>velocity;
energy=(1.0/2.0)*(mass*velocity)*2;
cout<<"\n mass = "<<mass<<"kg";
cout<<"\n velocity = "<<velocity<<"m/s";
cout<<"\n kinetic energy = "<<energy<<"kgm/s";
}

The above error can be solved by using namespace std which, is used to define the scope of cout and cin  and the context in which defined. In case if something is not declared in the current scope, it will be checked in std, using namespace std.

By using namespace std we can tell the compiler to print or display the cout command directly and cin command to take the input. If we will not tell the compiler to use the namespace std, we have to declared the namespace for every outside object, function, variable, method we call.


Related Solutions

I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint:...
I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint: matches() should have returned true when item matches the item passed into IdLookup constructor. Here is my code. What changes should be made? /** * This class is a lookup that matches items whose id matches exactly * a specified id, to be passed into the constructor when creating * the instance of the lookup. * * @author Franklin University * @version 2.0 */...
I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user decide the size of a 2d array then fill said array. #include <iostream> using namespace std; typedef int* IntArrayPtr; int main(void) { int column, row, i, j; IntArrayPtr *arr_num = new IntArrayPtr[row]; cout << " " << endl << " This program creates a 2D array of dynamic memory, fills it with user entered numbers, " << endl << " Then prints the array...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
Hi, I am running C# in Vis. Studio 2019 community. Trying to get my program to...
Hi, I am running C# in Vis. Studio 2019 community. Trying to get my program to populate the username in the program after entered. I can enter a name and the three scores and average them as the program needs but the name is not adding next to the "Students name: " in the program. Any help would be appreciated and please place a note for what I am doing wrong. Thank you using System; using System.Collections.Generic; using System.Linq; using...
William is attempting to sign in to his operating system but he receives an error message...
William is attempting to sign in to his operating system but he receives an error message stating that he has entered an invalid password. How can he solve this problem?
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT