cin & cout in Visual C++ 2010

Hey Guys,
I just downloaded Visual C++ last night and copied my code from Dev-C++ to Visual C++. When I gave the command to Build, the following error came for every "cout" , "cin" and "endl" : undeclared identifier. Don't these work with Visual C++ 2010 as well ??? If not, then what should I do for input and output.

Thanks,
Tejas
Let's see the code.

Did you do the following?

1
2
#include <iostream>
using namespace std;
Or alternatively

1
2
3
using std::cin;
using std::cout;
using std:endl;

or

1
2
std::cin >> a;
std::cout << a << std::endl;


The Visual C++ compiler is a lot more strict about writing correct C++.
well it is still giving build errors ...... and I am having the same problem as above for

srand (time (NULL))

It says

'time' : identifier not found
The function time lives in the header file ctime. Did you put

#include <ctime>

in your code?

If you're having the same problems as above, did you try the same solution as above?
Last edited on
closed account (1vRz3TCk)
shacktar wrote:
Let's see the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Indian Cricket Coach - 2011.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Indian Cricket Coach - 2011.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_INDIANCRICKETCOACH2011, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_INDIANCRICKETCOACH2011));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_INDIANCRICKETCOACH2011));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_INDIANCRICKETCOACH2011);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <string>
using namespace std;
class Team
{
      public:
      std::string Name;
      std::string Location;
      std::string GoverningBody;
      std::string FirstTestMatch;
      std::string FirstODIMatch;
      std::string FirstT20Match;
      std::string PresidentOfGovBody;
      std::string TestCaptain;
      std::string ODICaptain;   
      std::string T20Captain;
};
      
      class TestTeam
      {
      public:
      std::string Name;
      std::string DateOfBirth;
      std::string MajorTeams;
      std::string BattingStyle;
      std::string BowlingStyle;
      unsigned int TestMatchesPlayed;
      unsigned int TestInnings;
      unsigned int TestRunsScored;
      double TestBattingAvg;
      unsigned int TestHighScore;
      unsigned int TestHundreds;
      unsigned int TestFifties;
      double TestSR;
      unsigned int TestCatches;
      unsigned int TestWicketsTaken;
      double TestBowlingAvg;
      double TestBowlingSR;
      double TestBowlingER;
      };
      
      class ODITeam
      {
      public:
      std::string Name;
      std::string DateOfBirth;
      std::string MajorTeams;
      std::string BattingStyle;
      std::string BowlingStyle;
      unsigned int ODIMatchesPlayed;
      unsigned int ODIInnings;
      unsigned int ODIRunsScored;
      double ODIBattingAvg;
      unsigned int ODIHighScore;
      unsigned int ODIHundreds;
      unsigned int ODIFifties;
      double ODISR;
      unsigned int ODICatches;
      unsigned int ODIWicketsTaken;
      double ODIBowlingAvg;
      double ODIBowlingSR;
      double ODIBowlingER;
      };

int main()
{
     system("color 17");
     

     Team Team[8];
     Team[1].Name = "India";
     Team[1].Location = "Indian Subcontinent";
     Team[1].GoverningBody = "BCCI";
     Team[1].PresidentOfGovBody = "Shashank Manohar";
     Team[1].FirstTestMatch = "June 25, 1932 vs England";
     Team[1].FirstODIMatch = "July 13, 1974 vs England";
     Team[1].FirstT20Match = "December 1, 2007 vs South Africa";
     Team[1].TestCaptain = "MS Dhoni";
     Team[1].ODICaptain = "MS Dhoni";
     Team[1].T20Captain = "MS Dhoni";
     
     Team[2].Name = "Australia";
     Team[1].Location = "Indian Subcontinent";
     Team[1].GoverningBody = "BCCI";
     Team[1].PresidentOfGovBody = "Shashank Manohar";
     Team[1].FirstTestMatch = "June 25, 1932 vs England";
     Team[1].FirstODIMatch = "July 13, 1974 vs England";
     Team[1].FirstT20Match = "December 1, 2007 vs South Africa";
     Team[1].TestCaptain = "MS Dhoni";
     Team[1].ODICaptain = "MS Dhoni";
     Team[1].T20Captain = "MS Dhoni";

     ODITeam ODIPlayer[8][11];
     // Virender Sehwag - Player 1
     ODIPlayer[1][1].Name = "Virender Sehwag "; 
     ODIPlayer[1][1].DateOfBirth = "October 20, 1978";
     ODIPlayer[1][1].MajorTeams = "India, Asia XI, Delhi Daredevils, Delhi, ICC World XI";
     ODIPlayer[1][1].BattingStyle = "Right-hand Opener";
     ODIPlayer[1][1].BowlingStyle = "Right-arm Offbreak";
     ODIPlayer[1][1].ODIMatchesPlayed = 236;
     ODIPlayer[1][1].ODIInnings = 230;
     ODIPlayer[1][1].ODIRunsScored = 7760;
     ODIPlayer[1][1].ODIBattingAvg = 35.1;
     ODIPlayer[1][1].ODIHighScore = 175;
     ODIPlayer[1][1].ODIHundreds = 14;
     ODIPlayer[1][1].ODIFifties = 37;
     ODIPlayer[1][1].ODISR = 104.1;
     ODIPlayer[1][1].ODICatches = 87;
     ODIPlayer[1][1].ODIWicketsTaken = 92;
     ODIPlayer[1][1].ODIBowlingAvg = 42.4;
     ODIPlayer[1][1].ODIBowlingSR = 45.9;
     ODIPlayer[1][1].ODIBowlingER = 5.3;
     

     // Sachin Tendulkar - Player 2
     ODIPlayer[1][2].Name = "Sachin Tendulkar"; 
     ODIPlayer[1][2].DateOfBirth = "April 24, 1973";
     ODIPlayer[1][2].MajorTeams = "India, Asia XI, Mumbai, Mumbai Indians, Yorkshire";
     ODIPlayer[1][2].BattingStyle = "Riht-hand Bat; Test - Middle Order; ODI - Opening";
     ODIPlayer[1][2].BowlingStyle = "Right-arm Offbreak, Legbreak Googly";
     ODIPlayer[1][2].ODIMatchesPlayed = 453;
     ODIPlayer[1][2].ODIInnings = 442;
     ODIPlayer[1][2].ODIRunsScored = 18111;
     ODIPlayer[1][2].ODIBattingAvg = 45.2;
     ODIPlayer[1][2].ODIHighScore = 200;
     ODIPlayer[1][2].ODIHundreds = 48;
     ODIPlayer[1][2].ODIFifties = 95;
     ODIPlayer[1][2].ODISR = 86.3;
     ODIPlayer[1][2].ODICatches = 136;     
     ODIPlayer[1][2].ODIWicketsTaken = 154;
     ODIPlayer[1][2].ODIBowlingAvg = 44.4;
     ODIPlayer[1][2].ODIBowlingSR = 52.2;
     ODIPlayer[1][2].ODIBowlingER = 5.1;
  
     int x;
     int y;
     int z;
     int k;
     int Score;
     int Wicket;
     int BallResult[6];     
     for(y = 1; y <= 2; y++)
     {
           cout << Team[y].Name << "- Bowling Economy Rates" << endl;
                      for(x = 1; x <= 11; x++)
                {                

                                 cout << ODIPlayer[y][x].Name << " - \t" << ODIPlayer[y][x].ODIBowlingER << endl;
                                 
                }
     cout << "   " << endl;
     }
     srand( time(NULL) );
     for(x = 1; x <=10; x++)
     {
           
     for(z = 1; z <=6; z++)
     {
           BallResult[z] = rand() % 10000 + 1;
     if (BallResult[z] >= 0 && BallResult[z] < 100)
        k = 7;
     else
     if (BallResult[z] >= 100 && BallResult[z] < 900)
        k = 4;
     else
     if (BallResult[z] >= 900 && BallResult[z] < 1200)
        k = 6;
     else
     if (BallResult[z] >= 1200 && BallResult[z] < 5400)
        k = 0;
     else
     if (BallResult[z] >= 5400 && BallResult[z] < 9600)
        k = 1;
     else
     if (BallResult[z] >= 9600 && BallResult[z] < 10000)
        k = 2;
        
     if (k < 7)
          {
               cout << k << " ";
          Score = Score + k;
          }
          else
     if (k = 7)
          {
               cout << "W ";
          Wicket = Wicket + 1;
          }
        }
    cout << endl;
    cout << "-----------------" << endl;
;}
    cout << Score << "/" << Wicket << endl;
     
     cin.ignore();
     return 0;
}
The entire code is partitioned into the above two responses ... the code is working fine when I use Dev-C++ but not in Visual C++
Moschops said
Or alternatively


using std::cin;
using std::cout;
using std:endl;

or

std::cin >> a;
std::cout << a << std::endl;

The Visual C++ compiler is a lot more strict about writing correct C++.


@Moschops; I tried both alternatives, it is working only for endl; but not for cout and cin, it says std has no member 'cout'. Please help.
Last edited on
Have you included <iostream>?
Oh shit I suck ..... I thought I had
Topic archived. No new replies allowed.