Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switch case
Author Message
MetalGenius Offline
Novi korisnik
*

Posts: 46
Joined: Oct 2011
Reputation: 3
Post: #1
Sad Switch case
Ne mogu da skontam zasto ne ide Ccc

Code:
#include <iostream>
using namespace std;
int main ()
{
    int n,s=0,x;
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>x;
        s+=x;
    }
      
  
  switch (s)
  {
  case s<=20 :
  cout<<1;
  break;
  
  case s>=21 && s<=40:
  cout<<2;
  break;
  
  case s>=41 && s<=60:
  cout<<3;
  break;
  
  case s>=61 && s<=80:
  cout<<4;
  break;
  
  default:
  cout<<5;
  break;
}

return 0;

}

A zadatak ide da se ucita broj n, n brojeva i ovisno ozbiru brojeva ispise 1,2,3,4 ili 5.
Ne kontam Ccc
E da, stalno mi pise 's cannot appear in constant expression' ili tako nesto. Bilo da sve ove uslovcice stavim u zagrade ili ne. Je l' mi moze neko reci u cemu je problem?
Ccc Ccc Ccc

I can't escape myself.
09-11-2011 01:53 AM
Find all posts by this user Quote this message in a reply
danielvast Offline
Forumaš
***

Posts: 432
Joined: Dec 2009
Reputation: 22
Post: #2
RE: Switch case
Neide zato sto switch iskaz nije namjenjen za ispitivanje dosega (manje, vise...) on samo uzima varijablu i uspoređuje sa konstantnim vrijednostima:

Code:
switch (s) {
case 1:
break;
case 2:
break;
---
---
}

Jedini nacin da rijesis ovaj problem je preko if - then iskaza ili da nabrojis sve caseove od npr. 0-20 i onda stavis break (ali to za ovaj tvoj problem bi rezultiralo kodom od cirka 300 linija koda). Namigivanje

"When all else fails, read the manual."
(This post was last modified: 09-11-2011 04:38 PM by danielvast.)
09-11-2011 04:28 PM
Find all posts by this user Quote this message in a reply
MetalGenius Offline
Novi korisnik
*

Posts: 46
Joined: Oct 2011
Reputation: 3
Post: #3
RE: Switch case
Aaaaa... znaci u tom je caka. Hvala. Al' bas He he
Ali ne moram sve caseove. Uspjelo je preko ifa, samo trebam npr.:

Code:
if (s>=21 && s<=40)
{
...
}

i tako za ostala...4 uslova.

Hvala ionako He he

I can't escape myself.
10-11-2011 01:55 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)