This question already has answers here:
How to break out of a loop from inside a switch?
(20 answers)
Closed 2 months ago.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
while(1)
{
scanf("%d", &a);
switch(a)
{
case 0;
break;
}
}
return 0;
}
If I have a loop and a switch inside it, I want to stop both of them. That's because a break just stops the switch.
In the program, there is a loop that asks for a variable and then a switch case where if the variable is 0, the loop and the switch case must stop.
I tried to add a variable for the loop and change it in the case for stopping the loop, but it doesn't work.
There are multiple ways to do this. Three follow.
Use a goto:
while (1)
{
scanf("%d", &a);
switch (a)
{
case 0:
goto AfterWhile;
}
}
AfterWhile:
Build code into the loop:
_Bool KeepGoing = 1;
while (KeepGoing)
{
scanf("%d", &a);
switch (a)
{
case 0:
KeepGoing = 0;
break;
}
}
Encapsulate in a function and use return:
void DoTheLoop(void)
{
while (1)
{
int a;
scanf("%d", &a);
switch (a)
{
case 0:
return;
}
}
}
Related
I am new in programming and try to learn by own ,i have an error in my code,i don't understand it's a syntax error or i've done smth wrong.I used 3 equations with it's condition and made it in for isntruction,while,do-while,if-else,with switch.After i introduce variable a it show me an error "exited with non-zero status".
#include <stdio.h>
#include <math.h>
int main() {
float a,x,b;
float L;
int m;
printf("Enter variables a,x,b:");
scanf("%f%f%f,&a,&x,&b");
printf("For using for instruction (enter 1)");
printf("For using while instruction (enter 2)");
printf("For using do-while instruction (enter 3)");
printf("For using ef instruction (enter 4)");
scanf("%d,&m");
switch(m){
case 1:
for (x=0;x<1.2;x++)
{
L=2*cos(x-(3.14/6));
}
for (x=0;x>= 1.2 && x<=3.9;x++)
{
L=x*x/(a+cos(powf((x+b),3)));
}
for (x=0;x>3.9;x++)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
break;
case 2:
while (x<1.2)
{
L=2*cos(x-(3.14/6));
}
while (x>= 1.2 && x<=3.9)
{
L=x*x/(a+cos(powf((x+b),3)));
}
while (x>3.9)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
break;
case 3:
do
{
L=2*cos(x-(3.14/6));
}
while (x<1.2);
do
{
L=x*x/(a+cos(powf((x+b),3)));
}
while (x>= 1.2 && x<=3.9);
do
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
while (x>3.9);
break;
case 4:
if (x<1.2)
{
L=2*cos(x-(3.14/6));
}
else
{
printf("First statement is false");
}
if(x>= 1.2 && x<=3.9)
{
L=x*x/(a+cos(powf((x+b),3)));
}
else
{
printf("Second statement is false");
}
if(x>3.9)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
else
{
printf("Third statement is false");
}
break;
default:
printf("\nNo right choices\n");
}
printf("Your answer is: L = %.3f,L");
}
Your problem is that your scanf arguments are not formatted correctly.
Instead of scanf("%f%f%f,&a,&x,&b"); use scanf("%f%f%f",&a,&x,&b);. Same in the second scanf.
The variables addresses are parameters, not part of the string.
When you call it, scanf finds the first %f but it doesn't have any address to put the value into. Or more accuratly, it finds the value it needs from garbage (read about the stack and dynamic number of arguments), because you didn't insert it.
scanf("%f%f%f,&a,&x,&b"); should be like this scanf("%f%f%f",&a,&x,&b);. Please correct where ever you used scanf. Your code is not taking input from user due to wrong syntax. I have compiled and tried it is runnig correctly. Please change scanf syntax every where.
scanf("%f%f%f,&a,&x,&b") to scanf("%f%f%f",&a,&x,&b)
scanf("%d,&m"); to scanf("%d",&m);
printf("Your answer is: L = %.3f,L"); to printf("Your answer is: L = %.3f",L);
You are missing the return 0; Statement at the end of the main function. Since c main function is int main ()
In this code, why I am getting i = "some garbage value" as output? I see that i is being declared but value = 10, not assigned. Why ?
main()
{
int a =1;
switch (a)
{
int b = 10;
case 1: printf ("b = %d \n", b);
break;
}
b is not being initialized. The assignment is outside of any case in the switch, so it picks whatever was in the stack at that point.
If you want a variable inside a case statement, the right way to do it is:
switch(a)
{
case 1:
{
int b=10; //start a new block scope
printf("b=%d",b);
}
break;
}
Im writing a program to give the user options whether they want to:
Add random numbers to an array
Print array
Search for an element in an array
Left shift array
These have to be in separate functions and it needs to b recursive and until the user wants to finish it keeps running my code is:
int main()
{
int array[M][N];
int ans;
puts("Please enter what you would like to do:\n
1: Create an array with random values\n
2: Print Array\n
3: Search for a number in an array\n
4: Shift each value to the left");
scanf("%d",&ans);
switch(ans) {
case 1:
PopulateRandom2D(array);
break;
case 2:
PrintArray(array);
break;
case 3:
LinearSearch2D(array);
break;
case 4:
LeftShift(array);
break;
default:
puts("Goodybye");
return 0;
}
main();
return 0;
}
void PopulateRandom2D(int array[][N])
{
int r,c;
srand(time(NULL));
for(r = 0; r < M; r++) {
for(c = 0; c < N; c++) {
array[r][c] = 1 + (rand() % (M * N));
}
}
}
After i call the function i need the user to enter another command and call a different function from the user input. Ive been experimenting by first hitting 1 so it fills the array with numbers and then hitting 2 so it will hopefully print out that array but all i get are huge numbers in the array. I don't think the function is editing the array in main correctly so main doesn't get the array with random values but how do i fix this?
The code below works:
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
const int M = 10;
const int N = 20;
void PrintArray(int array[][N]) {}
void LinearSearch2D(int array[][N]) {}
void LeftShift(int array[][N]) {}
void PopulateRandom2D(int array[][N])
{
int r, c;
srand(time(NULL));
for (r = 0; r < M; r++) {
for (c = 0; c < N; c++) {
array[r][c] = 1 + (rand() % (M * N));
}
}
}
int main()
{
int array[M][N];
int ans;
while (true)
{
puts("Please enter what you would like to do:"
"\n1: Create an array with random values"
"\n2: Print Array"
"\n3: Search for a number in an array"
"\n4: Shift each value to the left"
"\n5: Quit");
scanf("%d", &ans);
switch (ans) {
case 1:
PopulateRandom2D(array);
break;
case 2:
PrintArray(array);
break;
case 3:
LinearSearch2D(array);
break;
case 4:
LeftShift(array);
break;
default:
puts("Goodybye");
return 0;
}
}
return 0;
}
I leave it to you to fill in the other functions.
You're currently recursively calling main(). In each iteration of calling main(), you'll create a new array on the stack.
This isn't what you want.
Instead, wrap your code in a while(true) { ... } loop. It would look something like this:
#include <stdbool.h>
int main()
{
int array[M][N];
int ans;
while (true) {
puts("Please enter what you would like to do:\n
1: Create an array with random values\n
2: Print Array\n
3: Search for a number in an array\n
4: Shift each value to the left");
scanf("%d",&ans);
switch(ans) {
case 1:
PopulateRandom2D(array);
break;
case 2:
PrintArray(array);
break;
case 3:
LinearSearch2D(array);
break;
case 4:
LeftShift(array);
break;
default:
puts("Goodybye");
return 0;
}
}
return 0;
}
#include <stdio.h>
int main()
{
int input, counter,value;
int ABC[3];
counter = 0;
scanf("%d", &input);
switch (input)
{
case 1:
if (counter >=4)
{
printf("Error\n");
}
scanf("%d", &value);
ABC[counter]= value;
printf("ABC[%d] is %d \n", counter, ABC[counter]);
counter++;
main();
break;
case 2: //do anything
main();
break;
default:
printf("a is anything\n");
break;
}
return 0;
}
I want to put in array ABC every time i choose case 1 a value, until array ABC is full. My problem is, that i can only enter in this programm values into ABC[0] . Is there any way, to remember the value of counter, so it's not always 0? Maybe using if-statement? But how to formulate an if-statement in this programm, which is only once in the beginning true?
But ABC should also be allowed to have empty space
After "counter++" you are re-calling your "main" function, that reinitializes your counter to 0. You should use a loop:
int counter = 0;
do
{
// Your scanf goes here
switch(scanf_result)
{
// Add your case labels here
default: // Incorrect input? Let's start again!
continue;
};
counter++;
}
while(counter < 3);
i think that declaring counter outside the main function, would make it global scope.
in this way everytime you call main(); you reset the value to 0.
so write int counter = 0 before the main declaration, at row number 2.
I am trying to use a loop to print out a repetitive song, "this old man"
The first verse is:
This old man, he played one
He played knick-knack on my thumb
This old man came rolling home
This song repeats to ten, varying the two terms in italicize
one -> two++ and thumb -> another item such as shoe, knee, etc.
Here is my code so far:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string change1 (int i);
int main (void)
{
for (int i = 1; ; 1 < 11; i++)
{
printf ("This old man, he played ");
change1(i);
printf("He played knick-knack on my %s\n\n", s1);
}
return 0;
}
string change1(int i)
{
string s1;
switch(i)
{
case 1:
{
printf("one\n");
s1 = "thumb";
}
break;
case 2:
{
printf("two\n");
s1 = "shoe";
}
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
printf("ill add these cases later");
}
}
This gives me an error message of: "control reaches end of non-void function"
I also got an undeclared variable s1 error but I declared it in the function.
You could simplify your program to an actual C program, rather than C++
int main (void)
{
int i;
char* items[] = {"thumb", "shoe", "", "", "", "", "", "", "", ""};
char* numbers[] = {"one", "two", "three","four","five","six","seven","eight","nine","ten"};
for (i = 0; i < 10; i++)
{
printf ("This old man, he played %s\n", numbers[i]);
printf("He played knick-knack on my %s\n\n", items[i]);
}
return 0
}
In C++ variables have scope. A variable is generally visible inside the curly braces where it is declared; outside these brackets the variable does not exist.
That is why you cannot use s1 from change1 inside the loop: you need to return a value (best choice in your situation), or use a variable that is in scope in both change1 and main.
printf ("This old man, he played ");
printf("He played knick-knack on my %s\n\n", change1(i));
...
string change1 (int i) {
string s1;
switch (i) {
...
}
return s1;
}
Note that you do not need a switch statement to implement change1: when the code is so uniform, you may be better off with an array:
const char *strings[] = {"thumb", "shoe", ...};
change1 needs to return the string it decided on. And main has to assign the return value to a variable, because as originally written s1 is local to the change1 function.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string change1 (int i);
int main (void)
{
for (int i = 1; ; 1 < 11; i++)
{
printf ("This old man, he played ");
string s1 = change1(i);
printf("He played knick-knack on my %s\n\n", s1);
}
return 0;
}
string change1 (int i)
{
string s1;
switch (i)
{
case 1:
{
printf("one\n");
s1 = "thumb";
}
break;
case 2:
{
printf("two\n");
s1 = "shoe";
}
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
printf("ill add these cases later");
}
return s1;
}
use return statement at the end of switch class
return s1;