1

In my main.cpp all of my cout's and cin's have errors.

/**
* Description: This program demonstrates a very basic String class.  It creates
* a few String objects and sends messages to (i.e., calls methods on)
* those objects.
*
*/
//#include <iostream>
#include "mystring.h"
//using namespace std;

/* Function Prototypes */

void Display(const String &str1, const String &str2, const String &str3);


/*************************** Main Program **************************/

int main()
{
  String str1, str2, str3;   // Some string objects.
  char s[100];               // Used for input.

  // Print out their initial values...

 cout << "Initial values:" << endl;
 Display(str1, str2, str3);

My main.cpp cannot not be changed, so my question is, how can I fix this error, what do I have to add to my header file and implementation file?

Joe
  • 79
  • 1
  • 1
  • 7

2 Answers2

5

In my main.cpp all of my cout's and cin's have errors.

You simply need to include <iostream> header file, and use std with cout and cin:

#include <iostream>
//^^
int main()
{
    std::cout << "Initial values: "<< std::endl;
    //^^
}
taocp
  • 23,276
  • 10
  • 49
  • 62
  • wow, I feel stupid but instead of using std, can't I just use using namespace std;? – Joe Jul 06 '13 at 03:28
  • @Joe You can do `using namespace std`, but it is considered bad practice. so it is better to use `std::cout` – taocp Jul 06 '13 at 03:29
  • 1
    @Joe: Just to further elaborate, there is a good reason why you shouldn't use that. Say you download a C++ library that has a cout function. The compiler could confuse the two. And, if you liked this answer, you should accept it. – kirbyfan64sos Jul 06 '13 at 03:46
  • Hmm, thats pretty neat, I never knew that "using namespace std;" could have a draw back. – Joe Jul 06 '13 at 03:49
4

You have iostream header commented out here:

//#include <iostream>

You also need to add std::, this:

cout << "Initial values:" << endl;

should be:

std::cout << "Initial values:" << std::endl;

I see that you have using namespace std; commented out. I would advise against using namespace std;, it may save you some typing but it is considered bad practice and can cause problem later on.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740