-2

The obstacle here is to find a number with the same decimals and do some math with them.

What I want to achieve here is just to make the switch/case statement work.

I just started programming (ಥ_ಥ)

What did I do wrong here?

I can't find the issue with the logic...

#include <bits/stdc++.h>

using namespace std;


int main() {

    int a, add[5]={0};
    for(int i=1; i<=10000; i++){
  switch(i)
        case 1:
            add[0]+=i;
            break;
        case 10:
            add[1]+=i;
            break;
        case 100:
            add[2]+=i;
            break;
        case 1000:
            add[3]+=i;
            break;
        case 10000:
            add[4]+=i;
            break;
    }
    cout<<add[0]<<"\n"<<add[1]<<"\n"<<add[2]<<"\n"<<add[3]<<"\n"<<add[4]<<"\n";
    return 0;
}
HarryPark
  • 1
  • 2
  • What are you trying to do with this loop? It iterates from 1 to 10000 and does...nothing? – John Zwinck Feb 20 '21 at 07:42
  • 1
    What do you mean that this code 'doesn't work'? What did you expect it to do? – john Feb 20 '21 at 07:42
  • Did you tried adding a cout just to check if things are working? Do you really need that switch statement for that? – SMA Feb 20 '21 at 07:43
  • The code doesn't do anything, but apart from that no-one can really say what is wrong with it unless you explain what you wanted it to do. – john Feb 20 '21 at 07:44
  • The reason why I didn't write anything else is that the switch statement itself doesn't work... The code just breaks down and stops working... but here you have it >> Just wrote some code to help you guys understand more easily – HarryPark Feb 20 '21 at 08:05
  • @HarryPark The code you have posted above would not break down and stop working. Your error must be somewhere else. – john Feb 20 '21 at 08:09
  • @HarryPark Acutally I was wrong, in the code above you have missingg `{}` after the switch. So the latest code does notcompile. But so far you haven't posted any code that 'breaks down'. – john Feb 20 '21 at 08:13
  • @HarryPark As you can see [here](https://onlinegdb.com/rJNzrBAW_) once the syntax errors are fixed this code runs perfectly ok. – john Feb 20 '21 at 08:16
  • please edit post and put correct code that compiles, and describe what you expect from code and what you get actually, just "break down" is too general. – ivan.ukr Feb 20 '21 at 08:17
  • @HarryPark I'm not doubting that your problem is real, but so far you haven't provided any information that indicates what it actually is. – john Feb 20 '21 at 08:17
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). The combination of both bad habits can be even more harmful. – prapin Feb 20 '21 at 08:28
  • *"to make the switch/case statement work"* -- The first step towards this goal is to define what would constitute "working". What is this particular statement supposed to accomplish? How do you know that goal is not accomplished? Without text describing "working", we have to guess at what would be working based upon the code. The code's behavior corresponds exactly to how it was written, so we should conclude it works? Even though how it was written is probably not how you want it to be written? (We can't read your mind to see what you wanted this line to accomplish.) – JaMiT Feb 20 '21 at 08:34

2 Answers2

0

As mentioned, you are missing two braces { and }.

Just a quick run down on your code: you create a loop from 1 to 10,000 and the add array ends up with {1, 10, 100, 1000, 10000}. Don't know if this is by design.

int main() {

  int a, add[5] = {0};
  for (int i = 1; i <= 10000; i++) {
    switch (i) // Missing a {
      case 1:
        add[0] += i;
        break;
      case 10:
        add[1] += i;
        break;
      case 100:
        add[2] += i;
        break;
      case 1000:
        add[3] += i;
        break;
      case 10000:
        add[4] += i;
        break;
    // Missing a }
  }
  cout << add[0] << "\n" << add[1] << "\n" << add[2] << "\n" << add[3] << "\n" << add[4] << "\n";
  return 0;
}
Chris Happy
  • 7,088
  • 2
  • 22
  • 49
0

If statement worked as intended. Thanks for helping me.

#include <bits/stdc++.h>
int arr[10001]={0};
int main() {
    for(int i=1; i<10000; i++){
        if(i<10)
            arr[i+i]=1;
        else if(i<100)
            arr[i+i/10+i%10]=1;
        else if(i<1000)
            arr[i+i/100+(i%100)/10+i%10]=1;
        else if(i<10000)
            arr[i+i/1000+(i%1000)/100+(i%100)/10+i%10]=1;
        if(arr[i]==0)
            std::cout<<i<<"\n";
    }
    return 0;
}
HarryPark
  • 1
  • 2