Having trouble with some of my cases

I'm having trouble finishing off my code. My case 2, 5, and 10 are not quite working

In my case 2 it works but it does not give the right output instead of giving the length of the word give the a string of letters and numbers. For example instead of hearing = 7 it gives hearing = Length[00F1F768] = 0 or something



This is my main function and then the needed void functions

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
198
199
200
201
202
203
204
205
#include <iostream>

// Function Prototypes=========================================================
// Do NOT change the signature of these function prototypes.
// If you do, your code will not compile with my altered main() function
// I suggest that you copy the prototypes below and then fill them in.
// ----------------------------------------------------------------------------
// Read in a line of text INCLUDING SPACES into a string.
// You may assume that the input will NOT exceed the maxLength available.
// Keep in mind that cin stops reading at a whitespace. See page 318.
void ReadString(char * c, int maxLength);

// Get the length of the string and store it in length
// Hint: How does C++ terminate a string? Look for that character!
void GetStringLength(char * c, int * length);

// PrintString - Just print the string in forward order using cout
void PrintString(char * const c);

// PrintStringBackwards - Print the string in reverse order using cout
void PrintStringBackwards(char * const c);

// Search the string for the test character. Return -1 if not found
int FindIndexOfCharacter(char * c, char testVal);

// Return a pointer to the character at the index given
char * GetValueAtIndex(char * const c, int index);

// Return the ascii integer value of the character at requested index
int GetIntegerValueOfIndex(char * c, int index);

// Print the hexadecimal value of the character at the requested index
void PrintHexValueAtIndex(char * c, int index);

// Make the entire string uppercase
void MakeUpperCase(char * c);

// Make the entire string lowercase
void MakeLowerCase(char * c);



// ============================================================================

//[BEGIN MAIN] -- Don't delete this line
int main()
{
  // Use these variables to test.
  // SIZE could change so make sure your code works with different sizes.
  const int SIZE = 80;
  char ca[SIZE];
  char * pc = ca;
  int fPrints = 0;
  int bPrints = 0;
  int lengthChecks = 0;

  // Your code below
  // =========================
  
 char selection = 'z';
       while(selection != '99')
       {
         std::cout << "[1] Test ReadString" << std::endl;
         std::cout << "[2] Test GetStringLength" << std::endl;
         std::cout << "[3] Test PrintString" << std::endl;
         std::cout << "[4] Test PrintStringBackwards" << std::endl;
         std::cout << "[5] Test FindIndexOfCharacter" << std::endl;
         std::cout << "[6] Test GetValueAtIndex" << std::endl;
		 std::cout << "[7] Test MakeUpperCase" << std::endl;
		 std::cout << "[8] Test MakeLowerCase" << std::endl;
		 std::cout << "[9] Test GetIntegerValueOfIndex" << std::endl;
		 std::cout << "[10] Test PrintHexValueAtIndex" << std::endl;
         std::cout << "[99] Quit" << std::endl;
         std::cout << "Selection: ";
         std::cin >> selection;
         std::cin.ignore();
         std::cout << std::endl;

         switch(selection)
          {
           case '1':
                ReadString(pc, SIZE);
                break;
           case '2':
			   
               {
             lengthChecks += 1;
             int length = 0;
             GetStringLength(pc, &length);
             std::cout << "Length[" << &lengthChecks << "]=" << length << std::endl;
             break;
           }
          
         case '3':
           {
              fPrints +=1;
                std::cout << "Forward["<<fPrints<<"]=";
                PrintString(pc);
                std::cout << std::endl;
                break;
          }
		 case '4':
                bPrints += 1;
                std::cout << "Backwards[" << bPrints << "]=";
                PrintStringBackwards(pc);
                std::cout << std::endl;
                break;
         case '5':
           {
		std:: cout << "[" << PrintString << "]\n";
		char test;
		int index = 0;
		std :: cout << "Get index of what?: ";
		std :: cin >>test;
		std::cout << "Location of [" << test << "] =" << FindIndexOfCharacter << std::endl;
             break;
           }
        case '6':
          {
			  
            int index = -1;
            char newChar = ' ';
            char * cPtr;
             std::cout << "What index?: ";
             std::cin >> index;
             std::cout << "New char?: ";
             std::cin >> newChar;
             cPtr = GetValueAtIndex(pc, index);
             *cPtr = newChar;
            break;
           }
		case '7':
			{
				std::cout << "\n";
				std::cout << "Testing function MakeUpperCase()...\n"
					<< "=============================\n";
				MakeUpperCase(pc);
				std::cout << std::endl;
				break;
			}
		case '8':
			{
				
				std::cout << "\n";
				std::cout << "Testing function MakeLowerCase()...\n"
					<< "=============================\n";
				MakeLowerCase(pc);
				std::cout << std::endl;
				break;
			}
		case '9':
			{
				int i;
				std :: cout << "What Index (0-" << GetStringLength << ")?:";
				std :: cin >> i;
				std::cout << "Integer Value of "<< pc[i] << " is " << GetIntegerValueOfIndex(pc, i) << "\n\n";
			break;
			}
		case '10':
			{
			int i;
				std :: cout << "What Index (0-" << GetStringLength << ")?:";
				std :: cin >> i;
                                 
				std :: cout <<PrintHexValueAtIndex <<"\n";
				
			break;
		 }
        case '99':
           break;
         default:
           break;
         }
      }
  // =========================
  // Your code above

  std::cout << "Press ENTER";
  std::cin.get();
  return 0;
}

   void PrintString(char * const c){
  char *p=c;
  while(*p!='\0'){
        std::cout<<*p;
        p++;
        }
}

int FindIndexOfCharacter(char * c, char testVal){
   char *t = c;
   int i = 0;
   while(*t != '\0'){
       if(*t == testVal) return i;
       i++;
       t++;
   }
}

void PrintHexValueAtIndex(char * c, int index)
{
std :: cin >> index;
std :: cout << "HEX= " << index << (int)c;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
 #include <iostream>
 
 using namespace std;
 
 int main() {
	 int Vxd = 9118;
	 int *Other = &Vxd;
	 
	 cout << Vxd << " @ " << &Vxd << endl;
	 cout << Other << " = " << *Other << endl;
	 
	 return 0;
 }


As the example shows in line 9, all you need to do is remove "&" for lengthChecks and then you'll get the value instead of the address where the value is stored.
For example instead of hearing = 7 it gives hearing = Length[00F1F768] = 0
Your code is:
 
std::cout << "Length[" << &lengthChecks << "]=" << length << std::endl;
where lengthChecks is of type int. So expression &lengthChecks is the address of variable lengthChecks. It's helpful to see addresses as hex, so the number's being printed as hex for you.

As to why length is zero and not seven, you'll need to see GetStringLength. But you don't post the code for that, so you're on your own with that.
Last edited on
Topic archived. No new replies allowed.