didn't find problem!!!

Hi , I'm a beggginer in c++
and i didn't find what a problem from this

while (n[p] < r]
{
cout << " The number " << n[p] << " is less than " << r << endl;
}

please help me .

the full program is

#include <iostream>
#include <string>

using namespace std;

string n[12];
int p;
int r;
int main()
{
cout << "Will introduce twelve real elements:\n";

for(p=0;p<=11;p++)
{
cout << "Introducing twelve real numbers:\n";
cin >> n[p];
}
cout << "Introducing real number:\n";
cin >> r;

while (n[p] < r]
{
cout << " The number " << n[p] << " is less than " << r << endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}
You may not compare an object of type int with an object of type std::string

So this code

1
2
3
4
while (n[p] < r]
 {
 cout << " The number " << n[p] << " is less than " << r << endl;
 }


is invalid. Moreover if this comparision would be allowed then the loop would be infinite if n[p] would be indeed less than r.
Last edited on
copy the for loop from above and replace while with if and ] must be ):
1
2
3
4
5
6
7
for(p=0;p<=11;p++)
{
if(n[p] < r)
{
cout << " The number " << n[p] << " is less than " << r << endl;
}
}



Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
use int n[12] instead of string n[12]
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
#include <iostream>
#include <string>

using namespace std;

string n[12];
int p;
int r;
int main()
{
cout << "Will introduce twelve real elements:\n";

for(p=0;p<=11;p++)
{
cout << "Introducing twelve real numbers:\n";
cin >> n[p];
} 
cout << "Introducing real number:\n";
cin >> r;
for(p=0;p<=11;p++)
    {
        if(n[p] < p)
    {
        cout << " the number " << n[p] << " is less " << r << endl;
    }
    }


using this , in compilation didn't show my last cout ...
what i was wrong ?
Topic archived. No new replies allowed.