Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C++
#include<stdio.h>
#include<conio.h>

int main()
{
int a;
int b=0;

printf("enter your age :");
scanf("%d",&a);

switch(a)
    case a<18:
 {  
    printf("you can vote !");
   break;
   
case a>18:
  do{ b=b+1;
      printf("go die lol hahhahahhahhaahhah, you cant vote");
    }while(b<100);
}
}


What I have tried:

I tried to combine loop and switch but its showing some errors
Posted
Updated 8-Aug-23 11:18am
v4
Comments
Rick York 17-Jan-23 11:55am    
The word is please.

You cant mix switch with comparison in C++. Learn about the if statement
C++
if(a < 18) {
print("learn to use google");
} else if(a > 18){
print("learn coding");
} else {
// you might also think about the case you missed ;-)
}
 
Share this answer
 
Comments
CPallini 17-Jan-23 13:55pm    
5.
A few things:
1) If you want help from people, it's a good idea to be polite. We do not work for you (and it's unlikely that anyone would, for long) so you are not in a position to give orders. Annoying those you want help from is not a good way to get what you want.

2) While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

3) Asking questions is a skill: saying "it don't work" gives nobody any information: we know you have a problem or your wouldn't be prostign here.
Start here: Asking questions is a skill[^] and think about what you need to know, and what you need to tell us in order to get help.

4) You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.
We all make mistakes.
And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!
So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at for KarstenK to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!
 
Share this answer
 
Comments
CPallini 17-Jan-23 13:55pm    
5.
OK, I have fixed it for you.
C
#include <stdio.h>
  
unsigned  can_vote(unsigned a)
{
  switch(a)
  {
    case 0:
      return 0;
    case 18:
      return 1;
  }

  if ( can_vote(a-1) )
  {
    printf("sorry, you can't vote\n");
    return 1;
  }
  return 0;
}


int main()
{
  unsigned a;

  printf("please, enter your age :");
  scanf("%u",&a);

  can_vote(a) ? printf("sorry, you can't vote\n") : printf("you can vote\n");

  return 0;
}
 
Share this answer
 
A switch statement does not perform any comparisons, but jumps to the jump label specified in the case.
In addition, it is common to open a curly bracket after switch.
The statement
C++
switch (a)
case a<18:

could alternatively be replaced by a function that performs the comparison.
The whole thing would then look like this :
C
int chkvote(int a)
{
	if (a < 18) return 0;
	else return 1;
};

switch (chkvote(a))
{
case 0:    // a < 18:
   ...

Further I would still note 2 points :
-The include <conio.h> does not belong to the C - standard library or ISO C and furthermore it is not needed here.
- With C still a return belongs to the program to terminate.
 
Share this answer
 
but you still have the vote age reversed

another way would be although seems pretty pointless program

if(a < 18){
    printf("you can vote !");
}
else {
    while (b < 100)
    {
        b++;
        printf("go die lol hahhahahhahhaahhah, you cant vote");
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900