vendredi 29 septembre 2017

C++ Comparing consecutive integers of a string

I am writing a program to determine if a given set of integers is increasing, decreasing, or neither.

For example a string of "123456" would be increasing, and "54321" is decreasing.

My solution:

string my_str = "12345";

f = my_str.at(0);
s = my_str.at(1);
t = my_str.at(2);
l = my_str.at(3);
p = my_str.at(4);

if(f<=s<=t<=l<=p){

cout << "The string is increasing" << endl;

}

 if(f>=s>=t>=l>=p){

cout << "The string is decreasing" << endl;

}

Now I am not sure if this were even to work, but I understand the concept I am just having trouble putting this into code on C++. So is this the best way to go about it?

Edit: I understand this code is in-complete its just supposed to be a generalization to help me get a better understanding. The small code I posted prints out increasing regardless of the inputs, and it's not reading the my_str input, I had thought it would just output increasing or decreasing after running the program.

Aucun commentaire:

Enregistrer un commentaire