For loop

#include <iostream>
using namespace std;
int main()

{
cout<<"ABC 123\n";
cout<<"Select the number that coincides with the alphabet.\n";
cout<<"For example, the number 7 should display the letter G.\n";
int number;
for (0 = ?; 1 = A; 2 = B; 3 = C; 4 = D; 5 = E; 6 = F; 7 = G; 8 = H; 9 = I; 10 = J; 11 = K; 12 = L; 13 = M; 14 = N; 15 = O; 16 = P; 17 = Q; 18 = R; 19 = S; 20 = T; 21 = U; 22 = V; 23 = W; 24 = X; 25 = Y; 26 = Z);
cout<<"Enter a number between 1 and 26: " << cin << number;
cout<<"number";
return 0;
}
I have been reading everything I can to fix this for days and I cannot find the answer. Someone please help me.
If this is an assignment, could you post the full question? Nevertheless, below is a quick info on for loops.

A for loop repeats a specific number of times. For example, if a loop asks the user to enter the sales amounts for each month in the year, it will iterate twelve times. In essence, the loop counts to twelve and asks the user to enter a sales amount each time it makes a count. A count-controlled loop must possess three elements:
1. It must initialize a counter variable to a starting value.
2. It must test the counter variable by comparing it to a maximum value. When the
counter variable reaches its maximum value, the loop terminates.
3. It must update the counter variable during each iteration. This is usually done by
incrementing the variable.

Here is an example of a simple for loop that prints “Hello” five times:
1
2
for (count = 0; count < 5; count++)
      cout << "Hello" << endl;

In this loop, the initialization expression is count = 0 , the test expression is count < 5 , and the update expression is count++.The body of the loop has one statement, which is the cout statement.


In the code below, I use an array and a do-while loop to identify the letter based on the user's input.
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
#include <iostream>
#include <cctype> // For toupper function.

int main()
{
    const int SIZE{ 26 };
    int number{ 0 };
    char tryAgain{ 'Y' };
    char alphabet[SIZE] = {'A', 'B', 'C', 'D', 'E', 'F',
	   'G', 'H', 'I', 'J', 'K',
	   'L', 'M', 'N', 'O', 'P', 'Q',
	   'R', 'S', 'T', 'U', 'V', 'W',
	   'X', 'Y', 'Z' };

    do
    {

	   std::cout << "ABC 123\n";
	   std::cout << "Select the number that coincides with the alphabet.\n";
	   std::cout << "For example, the number 7 should display the letter G.\n";

	   std::cin >> number;
	   while (number < 0 || number > SIZE)
	   {
		  std::cout << "Your input " << number << " is not valid\n";
		  std::cout << "Try again\n";
		  std::cin >> number;
	   }
	   std::cout << alphabet[number - 1] << std::endl; // Arrays element begins with 0 (Off-by-one rule).

	   std::cout << "Would you like to try again?\n";
	   std::cin >> tryAgain;

    } while (toupper(tryAgain) == 'Y');


    return 0;
}
I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
  int number;
  
  cout<<"ABC 123\n";
  cout<<"Select the number that coincides with the alphabet.\n";
  cout<<"For example, the number 7 should display the letter G.\n";
  
  cout<<"Enter a number between 1 and 26: ";
  cin >> number;
  cout << "\ncharacter: " << (char) (number + 64) << "\n\n";

  system("pause");
  return 0;
} 
Thank you very much Thomas1965. This solved my problem and taught me another way of doing it that I would never have thought of myself. You were a bigger help than you could have possibly imagined.

On another note, if you know anyone into the ide meteor I would like to email him/her.
Last edited on
Topic archived. No new replies allowed.