-3
#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.

Meenaz
  • 9
  • 6
  • 3
    What variable does your *first* `for` loop assign on its first iteration? And what variable does it assign to on its second iteration? And its third? – Adrian Mole Aug 25 '22 at 14:27
  • 1
    We're not going to debug your code for you. That's not how stack overflow works. You should write your own test cases and learn to use a debugger. Also refer to [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – Jason Aug 25 '22 at 14:30
  • Did you really want 2 for loops? – drescherjm Aug 25 '22 at 14:30
  • 2
    It's impossible for this code to execute the else block, `if(km>300){ ... } else if(km<=300)` One of those two conditions must be true, so it cannot execute the else block. – john Aug 25 '22 at 14:30
  • it's the variable sum. – Meenaz Aug 25 '22 at 14:30
  • 2
    This probably could have been sussed out faster with proper formatting. – sweenish Aug 25 '22 at 14:30
  • @Meenaz I think you missed the point of Adrian's comment. – john Aug 25 '22 at 14:31
  • @Meenaz This code has an obvious problem, the two for loops (there should only be one). However that is not the problem you are asking about, which doesn't seem like a real problem (to me at least). – john Aug 25 '22 at 14:32
  • @Meenaz So lots of good advice here, but nothing about the problem you are actually asking about. – john Aug 25 '22 at 14:33
  • 1
    What is the test case which is causing the wrong-ness? Have you tried stepping through the running code with a debugger? – Eljay Aug 25 '22 at 14:35
  • `for( i=0;i>km; }` repeatedly reads from the console into the variable `km`. Each time through the loop the read overwrites the previous value of `km`, so the result is that `km` holds the last value that was typed in, having discarded all the previous values. That's probably not what the code is supposed to do, but that's a guess, because the question doesn't say what the program should do. – Pete Becker Aug 25 '22 at 14:51
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Sep 25 '22 at 08:12

1 Answers1

1

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;
}
sweenish
  • 4,793
  • 3
  • 12
  • 23