• Register
1 vote
26 views

Problem:

I am a new student of c++ programming. I am taking an online-based course to learn it. I’ve written a pretty simple program that adds two numbers through a function. I am attaching my code snippet below, please have a look:

int promptNumber()
{
    int n;
    std::cout << "Number: ";
    std::cin >> n;
    return n;
}

void saveAnswer(int x)
{
    std::cout << "Answer: ";
    std::cout << n;
}

This is my io.cpp file. I’ve added both

#include <iostream>
#include "add.h"

header files starting on my main.cpp file but I can’t run the program. I keep getting an error: cout is not a member of std

Could any of you please shed some light on this?

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

You need to add the header file top of the io.cpp file too. Without the <iostream> header file the compiler never understands what to do with the codes. So, add the <iostream> header top of the io.cpp file then run the program. Then your program should run fine.

Good Day!

13 9 6
94,240 points

Related questions

0 votes
1 answer 114 views
114 views
Problem: I have been working on classes in C++ for about a few days. I am learning constructors with parameters. I was trying to run the following program in which I have declared two constructors, one without parameters and other with one parameter. The problem ... The program works perfectly when I comment out the error-generating statement. I have no idea what I am doing wrong. Please Help.
asked Nov 9, 2020 Code Learner 5.8k points
0 votes
2 answers 264 views
264 views
Problem: Which of the following is not a valid C++ identifier?
asked Mar 9, 2020 ArifulIslam 7.5k points
0 votes
1 answer 7 views
7 views
Problem: What is a const member function in C++? What is the syntax of a const member function? How do you declare a constant in C++? What does const after a function mean?How member function can be called on a const object (or via a reference-or-pointer-to-const), and that the member function cannot modify data members (unless they're marked mutable ?
asked Dec 23, 2020 Mashhoodch 9.1k points
0 votes
1 answer 50 views
0 votes
1 answer 96 views
96 views
Problem: I have written the following class: class Foo { private:     static float scale; public:     static float setScale(float scale); }; If I try to implement the setScale() function as shown below: float Foo::setScale(float scale) {    this->scale = ... still throws the same error. Now I am really confused about the above error message as my function in question is already marked as static.
asked Aug 13, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 9 views
9 views
Problem: Hello I am having some problems with the methods of a class in my program, I think it is due to the distribution in different header files, I wanted to know in which particular cases this error occurs? error: request by member 'init' in ' ... "renderer/renderer.h" void init(){ Renderer mainRender(); allegro_init(); install_keyboard(); mainRender.init(); } Hope someone could help!
asked Feb 7 sasha 8.7k points
0 votes
1 answer 15 views
15 views
Problem: I'm trying to compile using g++ and either the -std=c++11 or c++0x flags. However, I get this error cc1plus: error: unrecognized command line option "-std=c++11" g++ --version g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) ... Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
asked Dec 24, 2020 Mashhoodch 9.1k points
0 votes
1 answer 68 views
68 views
Problem: I am having only fundamental knowledge of g++. Now I am trying to compile my code using g++ and either the with the use of -std=c++11 or c++0x flags. But I am facing below error: cc1plus: error: unrecognized command line option "-std= ... . This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
asked Aug 11, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 266 views
266 views
Problem : I am a Computer Science Student. I am trying to learn the C++ programming. I want to execute a very simple averaging code written by me but I always face below error: Terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: ... larger than 0? So I am confused because of above error. Can some expert in C++ guide me in fixing above error for me.
asked May 14, 2020 Martin K 6.6k points
1 vote
1 answer 1.3K views
1.3K views
Problem : I am literally trying to start learning C++, and I have written very simple code in C++ as follows for your reference: string mySong(string songlist[], int songnum) { for (i = 0; i < songnum; i++) { output += songlist[i]; output += bone1; ... available answers make sense with what I know about programming so far. So I am looking for the pin point solution to my above C++ related error.
asked May 13, 2020 Martin K 6.6k points