Iterating through the contents of an array with a for loop

What am missing? I only get the message

#include <iostream>
#include <string>
#include <array>
using namespace std;

int main (void)
{
int index = 0;
string message = "hello all!";
for (index = 0; index < message.length(); index ++)
{
cout << message[index];
}

cout << endl;

}
Last edited on
you don't need a header to use an array, it's built into the language not part of a library or framework

1
2
3
4
5
6
7

  float grade[10];
  
  for(int i = 0; i < 10; ++i)
      cin >> grade[i];

I converted it, but it said the same thing as before that ISO C++ forbids comparison between pointer and integer

#include <iostream>
using namespace std;

int main(void)
{
float grade[10];

for(int i = 0; i < 10; ++i)
cin >> grade[i];
cout << "put in 10 grades\n";
if (grade >=90 && grade <= 100)
{
cout << "A\n";
}
else if (grade >= 80 && grade <= 89)
{
cout << "B\n";
}
else if (grade >= 70 && grade <= 79)
{
cout << "C\n";
}
else if (grade >= 60 && grade <= 69)
{
cout << "D\n";
}
else if (grade < 60 && grade >= 0)
{
cout << "F\n";
}
return(0);
}

Not sure what happened but I finally got it.
#include <iostream>
using namespace std;

int main(void)
{
float grade[10];
float average;
cout << "put in 10 grades\n";
for(int i = 0; i < 10; ++i)
cin >> grade[i];
average = (grade[0]+grade[1]+grade[2]+grade[3]+grade[4]+grade[5]+grade[6]+grade[7]+grade[8]+grade[9])/10;
if (average >=90 && average <= 100)
{
cout << "A\n";
}
else if (average >= 80 && average <= 89)
{
cout << "B\n";
}
else if (average >= 70 && average <= 79)
{
cout << "C\n";
}
else if (average >= 60 && average <= 69)
{
cout << "D\n";
}
else if (average < 60 && average >= 0)
{
cout << "F\n";
}
return(0);
}
The reason you got that compiler error was because you typed

1
2
3
4
if (grade >=90 && grade <= 100)
{
cout << "A\n";
}


this isn't legal, grade is actually a pointer to the first index of the grade array, so basically you are comparing a memory address with the number 90 which is not valid,

you need to specify which grade you want such as
1
2
3
4
5

if (grade[0] >=90 && grade[0] <= 100)
{
cout << "A\n";
}



That makes sense, I had average evaluated after so then it worked.
Topic archived. No new replies allowed.