Home > Mobile >  i'm getting an error in for loop and if statements:
i'm getting an error in for loop and if statements:

Time:08-26

#include <iostream>
using namespace std;

int main() {
int i,t,km,sum=0;
    std::cin >> t;
    for( i=0;i<t;i  ){
      cin>>km;  
    }
    for(i=0;i<t;i  ){
    if(km>300){
        sum=km*10;
        cout<<sum;
    }
   else if(km<=300){
        sum=300*10;
        cout<<sum;
    }
    else{
        cout<<"wrong!";
    }
    }
    return 0;
}

i'm not getting what's wrong with the code, when i'm entering number of test cases(t) as 1 it's running. but afterwars it's only executing the else block.

CodePudding user response:

We'll start with a mini code review:

#include <iostream>
using namespace std;  // Bad practice; avoid
/* Generally poor formatting throughout */
int main() {
int i,t,km,sum=0;  // Prefer each variable declared on its own line; i is unnecessary
    std::cin >> t;
    for( i=0;i<t;i  ){  // Overwrites `km` t times
      cin>>km;  
    }
    for(i=0;i<t;i  ){  // Only does work on the last number entered
    if(km>300){
        sum=km*10;  // You likely want to be adding on, not overwriting
        cout<<sum;
    }
   else if(km<=300){
        sum=300*10;
        cout<<sum;
    }
    else{  // Impossible to reach given your two other conditions
        cout<<"wrong!";
    }
    }
    return 0;
}

The comments have spelled a lot of this out. You overwrite km t times. You only end up running your check on the last value of km that was entered, t times. You likely want to process t inputs, and the way to do this is with a single loop instead of the two that you have.

You overwrite your sum instead of (I assume) adding on to it. You wanted to sum = and not just sum =. The computer is actually pretty dumb, but it's very good at doing exactly what you told it to do. It is incapable of guessing your intent. That's supposed to be comforting, but it can be interpreted many ways.

I would also recommend taking the time to come up with better variable names. It looks like you're doing some calculations, possibly having to do with legs of a trip, but it's unclear. Making your code harder to read by choosing horrible names like t won't allow others to easily help you, and it will make your own code seem like a foreign language after just a couple days. Help yourself and others out by choosing good names.

As I stated, you might have been able to figure this out on your own if the code was indented properly. Consistent and proper formatting is extremely important for readability. If you don't want to be bothered doing it yourself, tools like clang-format exist.

Here's your code again, touched up a bit. I took a couple guesses at intended behavior.

#include <iostream>

int main() {
  int t;
  int km;
  int sum = 0;
  std::cin >> t;
  for (int i = 0; i < t; i  ) {
    std::cin >> km;

    if (km > 300) {
      sum  = km * 10;
      std::cout << sum << '\n';
    } else if (km <= 300 && km > 0) {
      sum  = 300 * 10;
      std::cout << sum << '\n';
    } else {
      std::cout << "wrong!";
    }
  }
  
  return 0;
}
  • Related