Question on C++

Given the following program fragment, what happens to variable two if the letter ‘a’ is entered from the keyboard:


1
2
3
4
  float one;
char two;
cin >> one; //at this point, the letter ‘a’ is entered
cin >> two; 


My answer is that nothing happens because the variable two has not been assigned any value. Correct?
Please help
You could always compile it and see!
Correct
Thank you. How about this question-

Given the array int x [ ] = {1,2,3,4,5}, write a loop that replaces every other element with the value 2, starting with the first element.
Ans:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int x[] ={1,2,3,4,5};
for(int i=0;i < 5; i++) cout << x[i]/i*2 <<endl;
system("Pause");
return 0;
}
Please help.
Last edited on
It would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	const int size = 5;
	int x[size] = { 1,2,3,4,5 };

	//make everyother value 2
	for (int i = 0; i < size; i += 2)
	{
		x[i] = 2;
	}

	//output array
	for (int i = 0; i < size; i++)
	{
		cout << x[i] << endl;
	}

	return 0;
}
you're program will go to the infinite state
Topic archived. No new replies allowed.