Wondering if someone could guide me in the right direction. I think this code is right but can't figure out why my switch loop won't access my char array of grades and add it to an int array.

``````#include <stdio.h>

int main (void)

{

'A', 'B', 'C', 'D', 'f', 'I',
'b', 'C', 'd', 'F', 'I', 'a',
'C', 'D', 'F', 'I', 'A', 'B',
'D', 'F', 'I', 'A', 'B', 'c',
'I', 'D', 'F', 'F', 'A', 'B',
'a', 'b'};

char letters[6] = { 'A', 'B', 'C', 'D', 'F', 'I'};

for (i = 0; i < 32; ++i)
{
{
case'A':
case'a':
break;

case 'B':
case 'b':
break;

case 'C':
case 'c':
break;

case'D':
case'd':
break;

case'F':
case'f':
break;

case 'I':
case 'i':
break;

}

}

printf ("-----	--------------------\n");

for (i = 0; i < 6; ++i)

getchar();
return 0;
}``````

## All 9 Replies

I think if you look closely, you'll find that your switch statement is outside your loop.

``````for (i = 0; i < 32; ++i)

To include the switch you'll need to add braces like

``````for (i = 0; i < 32; ++i)
{
switch(something)
{...}
}``````

Not sure that was the problem. I compiled again with the the grade = 0 inside the braces and I get the same exact out put.

line 22 destroys the value of all the grades. Delete that line

line 24: only grade[32] is used in that switch statement. And grade[32] is wrong because it accesses an element of grade array beyond the bounds of the array. In orderwords, there is no grade[32] element. Elements are numbered 0 to 31.

What I think you want there is `switch( grade[i] )`

Before For loop initialize all elements of array hist_grade[6] to 0.

line 22: why are you destroying all values of grade to 0.. can't get the need of this line..
switch statement should me inside for loop..

line 24:

``switch(grade[32])``

should'nt it be

``switch(grade[i])``

line 28: how can you use it..

``++hist_grade[i];``

The values of i varies from 0 to 31 and hist_grade[6] can hold 0 to 5..
It should be

``++hist_grade[0];``

Line 38: ++hist_grade[2]; and so on...

Thanks guys... that was a huge help.

On a side note, what do you think about the Visual C++ Express compiler.

Say tuned for problems with printing 2D arrays up next :)

Thanks again.

>>On a side note, what do you think about the Visual C++ Express compiler.

Best one on the market for MS-Windows operating system, although more difficult to learn than Code::Blocks because if it's many whistles and bells.

what result is this supposed to print? i got a similar assignment and i'm not sure if the output is what i should expect...

----- --------------------
A1076285190
B1074806778
C1074792700
D1073831181
F-16121850
I-1079501539

``````#include <stdio.h>

int main (void)

{

'A', 'B', 'C', 'D', 'f', 'I',
'b', 'C', 'd', 'F', 'I', 'a',
'C', 'D', 'F', 'I', 'A', 'B',
'D', 'F', 'I', 'A', 'B', 'c',
'I', 'D', 'F', 'F', 'A', 'B',
'a', 'b'};

char letters[6] = { 'A', 'B', 'C', 'D', 'F', 'I'};

int i;

for (i = 0; i < 32; ++i)
{

{
case'A':
case'a':
break;

case 'B':
case 'b':
break;

case 'C':
case 'c':
break;

case'D':
case'd':
break;

case'F':
case'f':
break;

case 'I':
case 'i':
break;

}

}

printf ("-----	--------------------\n");

for (i = 0; i < 6; i++)

return 0;
}``````

output:

``````Grade	Number of Each Grade
-----	--------------------
A       6
B       6
C       4
D       5
F       6
I       5``````
commented: Do NOT write homework for others!!! -4

LOL, now that makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.