Modulus operator with array

So I just completed the first part of my assignment via someone elses thread so I figured its actually time for me to put a thread in the correct forum. I have to make a program print the even numbers from my array and output the even number and the corresponding letter that goes with that number. I have to use the Modulus operator to determine the even numbers within the array.

Example A = 0 so the output will be ... The even element = 0 The corresponding letter is A.

Here is my code so far.
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
// Arrays and Pointers program for Assignment week 4

#include<iostream>
#include<iomanip>

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

	int main()
	{                                        // All the arrays and pointers are declared here
		
		char pletter2[26]=  { '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' };
		char* pphrase = "PRINTING CONTENTS OF THE ARRAY";
		char* pphrase2 = "==============================";
		char* pphrase3 = "PRINTING CONTENTS OF THE ARRAY and adding x to every other element";
		char* pphrase4 = "==================================================================";
		char pphrase5 = 'x';
		char* pphrase6 = "PRINTING CONTENTS OF THE ARRAY USING MOD OPTION";
		char* pphrase7 = "===============================================";

		int number = 0;
		

		



		    cout<<endl;
			cout <<pphrase;
		    cout <<endl;
		    cout <<pphrase2;
			cout <<endl;

	     for(int i = 0; i < 26; i++) // this for statement prints all the elements of the array with a counter
		{
			
		    
			 cout <<pletter2[i];
			
		
		}
          
			cout <<endl;
			cout <<endl;
		    cout <<"Please enter a number between 1 and 26 that corresponds to a letter in the alphabet: "<<endl;
		    cout <<endl;
			cout <<"Example: The number 10 corresponds to the letter J."<<endl;
			cin >>number;

		 if ( number >=1 && number <=26)        // This if statement only happens if the number entered is within the range of the array which is 1-26
		 {
			     cout <<"The number you selected is: "<<number<<endl;
		         cout <<"The letter that corresponds with that number is: "<<pletter2[number-1]<<endl;
		 }
		          else 
		 {
			         cout<<"There was an error with your input please try again"<<endl;
		 }
		 
		 
		 
			
			 
		                                   // These are the outputs for the title
         cout <<endl;
         cout <<endl;
		 cout << pphrase3;
		 cout <<endl;
		 cout <<pphrase4;
		 cout <<endl;

			for(int i = 1; i < 2; i = i +2)    // This for loop replaces every other letter of the array starting with B and makes them "x"
            {
              
			    pletter2[i] = pphrase5;
				
                  
				 for(int j = 0; j<26; j = j+2) // This for loop only outputs every other letter of the array starting with A

					 cout <<pletter2[j]<<pletter2[i];
			         cout <<endl;
			}

		  cout <<endl;
	      cout <<endl;
		  cout <<endl; 
		  cout <<pphrase6;
		  cout <<endl;
		  cout <<pphrase7;


		  
		  int i;
		  
		  
		  for(int i=0; pletter2[i] !='\0' ; i++)   //This for loop goes thru all numbers
                
		  {
			   
			    
			   if ((pletter2[i] & 1) == 0)            // Checks if the number is even
              i=i+1;
           


		     cout <<endl;
			 cout <<"The even numbered element = "<<i<<"    Contents of element within array = "<<pletter2[i];
		     cout <<endl;
		  }
		  
		  
		  
		  
		  
		  
		  
		  
		 {
              
			 

			 
			   
				 

			
		}
		          
				
		 
		 
			return 0;
		}
	
		 


Right now it prints every number and says that it is even and it prints the correct corresponding letter.
Last edited on
I have gotten everything to run except the output goes past the number 24 which = y and goes all the way to 44 and produces a weird output. Could anyone possibly help me stop when it is done reading the array ? Thanks much and here is the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 for(int i=0; pletter2[i] !='\0' ; i++)   //This for loop goes thru all numbers
                
		  {
			   
			    
			   if ((pletter2[i] & 1) == 0)            // Checks if the number is even
              i=i+1;
           


		     cout <<endl;
			 cout <<"The even numbered element = "<<i<<"    Contents of element within array = "<<pletter2[i];
		     cout <<endl;
		  }

pletter2 doesn't have a null char to stop it... you'd be better doing the i < 26 thing, unless you add a null char.

actually, if you add a null char to the end (remember to add 1 to the size, but don't change any other code for iterating the array) you can output the entire char string with
std::cout << pletter2;
Topic archived. No new replies allowed.