C program only running the first loop and then stopping - c

Every time I try and run only the first loop runs. It ask how many items were sold and then just stops running. I'm not really sure what I did wrong. If anyone has any tips that would be great(story if there's some formatting issues, SO wouldn't let me post the question without them).
Everything else looks fine but if any other improvements could be made please let me know.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
i++;
}
return val;
}
void error()
{
printf("Error: Sales figures must be numbers.\n");
printf("Please try again.\n");
}
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if( isdigit(term[i]) == 0)
{
return false;
i++;
}
}
return true;
}
int main()
{
int sales[3][2], costs[3] = {3, 4, 1}, weekends[2] = {0, 0};
int i, j, val;
char term[100];
while (1)
{
printf("Number of Bagel sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Saturday: ");
scanf("%s", term);
if (isnumber(term) == false)
{
error;
}
else
{
sales[2][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Bagel sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[0][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[1][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error;
}
else
{
sales[2][1] = digit(term);
break;
}
}
for (i = 0; i < 2, i++;)
{
for (j = 0; j < 3, j++;)
{
weekends[i] += costs[j] * sales[i][j];
}
}
printf("\n");
for (i = 0; i < 3, i++;)
{
printf("%d", costs[i]);
}
printf(".");
for (i = 0; i < 3, i++;)
{
for (j = 0; j < 2, j++;)
{
printf("%d", sales[i][j]);
}
if (i == 0)
{
printf(" = ");
printf("%d %d", weekends[0], weekends[1]);
}
printf("\n ");
}
printf("\nTotal sales on Saturday: $%d", weekends[0]);
printf("\nTotal sales on Sunday: $%d", weekends[1]);
printf("\nTotal sales over the weekend: $%d", weekends[0] + weekends[1]);
return 0;
}

You have an infinite loop in isnumber(). It will return false if the first character is not a digit. But if the first character is a digit, it never increments i, so it keeps testing the first character repeatedly.
i++ should not be in the if statement.
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if(!isdigit(term[i]))
{
return false;
}
i++;
}
return true;
}
And as others have pointed out, you need to put () after error to call it.

As for your programming stopping; you use isdigit on line 30 without declaring it.
Along that, you use error everywhere without invoking it. Use error() to invoke the function and print the error-information.
Just a small formatting tip as well: instead of if (something == false) use if (!something)

Your use of error; is incorrect. You should write error(); instead.

Related

C program query

So this is my program and the number, name, address doesn't print again after single execution and also If the seat is already taken there should be "seat is taken already, please try again" which I'm confused about
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct passenger
{
char name[20];
char address[30];
int age;
};
struct passenger data;
struct rwcl
{
int row;
char col;
};
int clmn, i, j;
int arr[5][5];
struct rwcl number;
for(i=0;i<5;i++)
{
printf("Enter Your Name: ");
scanf("\n");
gets(data.name);
printf("Enter Your Address: ");
scanf("\n");
gets(data.address);
printf("Enter Your Age: ");
scanf("%d", &data.age);
printf("\nAll aboard! You may now choose your desired seat/s.");
while(i<5){
for(j=0;j<5;j++){
if (j == 0) {
arr[i][j] = i+1;
}
if(j == 1){
arr[i][j] = 'A';
}
if(j== 2){
arr[i][j] = 'B';
}
if(j == 3){
arr[i][j] = 'C';
}
if(j== 4){
arr[i][j] = 'D';
}
}
i++;
}
printrwcl:
printf("\n\n");
for(i=0;i<5;i++){
for(j=0;j<5;++j){
if(j == 0 ){
printf("%-5d", arr[i][j]);
}
else {
printf("%-5c", arr[i][j]);
}
}
if(j==5) {
printf("\n");
}
}
printf("\n");
rowselect:
printf("Choose a row between 1,2,3,4,5 or 6 for cancellation: ");
scanf("%d", &number.row);
if(number.row < 0 || number.row > 6) {
printf("\nPlease, re-enter. Thank you.\n");
goto rowselect;
}
if(number.row == 6) {
printf("Recorded, thank you.");
exit(0);
}
columnselect:
printf("Choose a letter between A,B,C,D: ");
scanf("\n");
scanf("%c", &number.col);
switch(number.col)
{
case 'A':
clmn = 1;
break;
case 'B':
clmn = 2;
break;
case 'C':
clmn = 3;
break;
case 'D':
clmn = 4;
break;
}
if(arr[number.row-1][clmn] == 'X')
{
printf("Seat is taken. Please choose a different one.");
}
else
{
printf("Seat %d%c has been reserved.", number.row, number.col);
arr[number.row-1][clmn] = 'X';
}
goto printrwcl;
}
}

Can't get my C program to print the output

When I go to print the output of the program everything shows up as zero. I think the variable aren't storing themselves, but I'm not totally sure. When I go to look over everything, it looks right but clearly isn't. Any help would be really appreciated. Sorry if the formatting seems a little off, Stack Overflow wouldn't accept it otherwise.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
}
i++;
return val;
}
void error()
{
printf("Error: Sales figures must be numbers.\n");
printf("Please try again.\n");
}
bool isnumber(char term[])
{
int i = 0;
while (term[i])
{
if( isdigit(term[i]) == 0)
{
return false;
i++;
}
}
return true;
}
int main()
{
int sales[3][2], costs[3] = {3, 4, 1}, weekends[2] = {0, 0};
int i, j, val;
char term[100];
while (1)
{
printf("Number of Bagel sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[0][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Saturday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[1][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Saturday: ");
scanf("%s", term);
if (isnumber(term) == false)
{
error();
}
else
{
sales[2][0] = digit(term);
break;
}
}
while (1)
{
printf("Number of Bagel sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[0][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Flatbread sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[1][1] = digit(term);
break;
}
}
while (1)
{
printf("Number of Muffin sales on Sunday: ");
scanf("%s", term);
if( isnumber(term) == false)
{
error();
}
else
{
sales[2][1] = digit(term);
break;
}
}
for (i = 0; i < 2, i++;)
{
for (j = 0; j < 3, j++;)
{
weekends[i] += costs[j] * sales[i][j];
}
}
printf("\n");
for (i = 0; i < 3, i++;)
{
printf("%d", costs[i]);
}
printf(".");
for (i = 0; i < 3, i++;)
{
for (j = 0; j < 2, j++;)
{
printf("%d", sales[i][j]);
}
if (i == 0)
{
printf(" = ");
printf("%d %d", weekends[0], weekends[1]);
}
printf("\n ");
}
printf("\nTotal sales on Saturday: $%d", weekends[0]);
printf("\nTotal sales on Sunday: $%d", weekends[1]);
printf("\nTotal sales over the weekend: $%d", weekends[0] + weekends[1]);
return 0;
}
You are not incrementing i in the loop. Your code for digit is:
int digit(char term[])
{
int i = 0;
int val = 0;
while (term[i] != '\0')
{
val = val * 10 + term[i] - '0';
}
i++; /* ---- this is outside the loop !! */
return val;
}
But it ought to look like:
int
digit(const char *term)
{
int val = 0;
while( *term != '\0' ){
val = val * 10 + *term - '0';
term += 1;
}
return val;
}

Check if input string is parenthesis completed

I'm trying to write a function, that would check for a matching parentheses.
For example, if the given string is "(1+1))" it would print false otherwise it's true.
However, in my code it's printing false no matter what the case is.
bool isMatched(char pran[]) {
bool completetd = true;
int count = 0;
for (int i = 0; pran[i] != '\0'; i++) {
if (pran[i] == '('){
count++;
}
else {
// It is a closing parenthesis
count--;
}
if (count < 0) {
// there are more Closing parenthesis
completetd = false;
break;
}
// If count is not zero, there are more opening parenthesis
if (count != 0) {
completetd = false;
}
}
return completetd;
}
int main() {
char arr[] = "((1+a))";
if (isMatched(arr)) {
printf("TRUE \n");
}
else {
printf("FALSE \n");
}
return 0;
}
I would appreciate any help.
You can try this not sure if this is what you are looking for.
bool isMatched(char pran[]) {
int open = 0;
int close = 0;
for (int i = 0; pran[i] != '\0'; i++) {
if (pran[i] == '('){
open++;
}
if (pran[i] == ')'){
close++;
}
}
// Check if both match
if(open == close){
return true;
}
return false;
}
int main() {
char arr[] = "((1+a))";
if (isMatched(arr)) {
printf("TRUE \n");
}
else {
printf("FALSE \n");
}
return 0;
}
By adding a
printf("got 1 (!\n"); next to count++;
and a
printf("got 1 )!\n"); next to count--;,
you get:
Got 1 (!
Got 1 (!
Got 1 )!
Got 1 )!
Got 1 )!
FALSE
This shows that you have a validation problem with your checking logic
As pointed-out in the comments, replace your else with else if (pran[i] == ')') { will fix that part for you.
But the real problem lies with your last validation.
Take it out of the for loop. It sets the value to false as soon as you detect a parenthesis.
Thus, take this:
// If count is not zero, there are more opening parenthesis
if (count != 0) {
printf("Count: %d\n",count);
completetd = false;
}
}
and make it this:
}
// If count is not zero, there are more opening parenthesis
if (count != 0) {
printf("Count: %d\n",count);
completetd = false;
}

Countries Grouping in c

People in a group are sitting in a group numbered 1 to N. It is known that people of same countries are sitting together.
Output is a single integer denoting no of distinct countries.
Input
4 (no of test cases)
2 (no of people in group)
1 1 ( in this there are 2 people from diff country)
2
1 3
7
1 1 2 2 3 3 3
7
7 7 7 7 7 7 7
Output should be
2
Invalid Data
4
1
My program:please tell me where is the error
#include<stdio.h>
#include<string.h>
int main()
{
int tcaseno,nopgrp,flag=0;
int arr[1000];
int count=0,i=0,j=0,t=0;
scanf("%d", &tcaseno);
t=tcaseno;
while(t>0)
{
scanf("%d\n", &nopgrp);
for (i = 0; i < nopgrp;i++)
{
scanf("%d", &arr[i]);
}
for (j = 0; j < nopgrp;j++)
{
if(arr[j]==1)
{
count++;
}
else if(arr[j]==2)
{
if(arr[j+1]==2)
{
count++;
}
else
{
flag=1;
}
}
else if(arr[j]==3)
{
if((arr[j+1]==3)&&(arr[j+2]==3))
{
count++;
}
else
{
flag=2;
}
}
else if(arr[j]==4)
{
if((arr[j+1]==4)&&(arr[j+2]==4)&&(arr[j+3]==4))
{
count++;
}
else
{
flag=3;
}
}
else if(arr[j]==5)
{
if((arr[j+1]==5)&&(arr[j+2]==5)&&(arr[j+3]==5)&&(arr[j+4]==5))
{
count++;
}
else
{
flag=4;
}
}
else if(arr[j]==6)
{
if((arr[j+1]==6)&&(arr[j+2]==6)&&(arr[j+3]==6)&&(arr[j+4]==6)&&(arr[j+5]==6))
{
count++;
}
else
{
flag=5;
}
}
else if(arr[j]==7)
{
if((arr[j+1]==7)&&(arr[j+2]==7)&&(arr[j+3]==7)&&(arr[j+4]==7)&&(arr[j+5]==7)&&(arr[j+6]==7))
{
count++;
}
else
{
flag=6;
}
}
else if(arr[j]==8)
{
if((arr[j+1]==8)&&(arr[j+2]==8)&&(arr[j+3]==8)&&(arr[j+4]==8)&&(arr[j+5]==8)&&(arr[j+6]==8)&&(arr[j+7]==8))
{
count++;
}
else
{
flag=7;
}
}
else if(arr[j]==9)
{
if((arr[j+1]==9)&&(arr[j+2]==9)&&(arr[j+3]==9)&&(arr[j+4]==9)&&(arr[j+5]==9)&&(arr[j+6]==9)&&(arr[j+7]==9)&&(arr[j+8]==9))
{
count++;
}
else
{
flag=8;
}
}
else if(arr[j]==0)
{
flag=9;
}
}
if(flag!=0)
{
printf("Invalid Data");
flag=0;
}
else
{
printf("%d\n",count);
count=0;
}
t--;
}
return 0;
}
if((arr[j+1]==9)&&(arr[j+2]==9)&&(arr[j+3]==9) ...)
You can simplify the above code with another for loop. Evidently you just want to see how many different numbers there are in the array.
Note that one of the main reasons to use C, or to learn C, is for efficiency. Therefore int arr[1000] is somewhat out of place because it allocates 4000 bytes. You may want to streamline that with malloc/free.
You should use printf to tell the user what to input.
I took some guesses on what you are trying achieve.
int tcaseno, nopgrp, error;
int count, i;
int *arr;
printf("no_test_cases: ");
scanf("%d", &tcaseno);
while(tcaseno > 0)
{
error = 0;
count = 1;
printf("no_people_in_group: ");
scanf("%d", &nopgrp);
if(nopgrp > 0 && nopgrp < 1000)
{
arr = malloc(nopgrp * sizeof(int));
printf("Enter %d numbers: ", nopgrp);
for(i = 0; i < nopgrp; i++)
scanf("%d", &arr[i]);
for(i = 1; i < nopgrp; i++)
{
if(arr[i - 1] > arr[i])
error = 1;
else if(arr[i - 1] != arr[i])
count++;
}
free(arr);
}
else
error = 1;
if(error)
printf("Invalid Data\n");
else
printf("Result: %d\n", count);
tcaseno--;
}

Loop incomplete

I just started learning coding for about 2 months because of the course im taking.
My code works (kinda) but after the 1st loop it wont show the 1st line of the main function("You have been given 20 pokeballs, embrak on you quest on becoming a Pokeman Master!!!:), and i dont know why!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int pokeball = 20;
int rand_num;
int PokemonCaught[5] = { 0, 0, 0 ,0, 0 };
int poke_captured = 0;
int rungame = 1;
int stopgame = 1;
int total;
int randomnum();
int encounter_rate();
int pokemon_met();
void BallThrow();
void CaughtPokemon();
void checkball();
void PokeSummary();
char exitno();
int clear();
int main()
{
printf("You have been given 20 pokeballs, embrak on you quest on becoming a Pokeman Master!!!\n");
getchar();
do
{
checkball();
encounter_rate();
pokemon_met();
} while (stopgame == 0);
PokeSummary();
return 0;
}
int randomnum()
{
srand(time(NULL));
}
int encounter_rate()
{
randomnum();
rand_num = rand() % 100;
}
int pokemon_met()
{
if (rand_num <= 30)
{
printf("A wild Margikarp appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 50)
{
printf("A wild Charmander appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 70)
{
printf("A wild Jigglypuff appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else if (rand_num <= 85)
{
printf("A wild Pikachu appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
else
{
printf("A wild Dragonite appeared!.\n");
printf("Press ENTER to throw a pokeball!. \n");
getchar();
rungame = 1;
BallThrow();
}
}
void checkball()
{
if (pokeball > 0)
{
stopgame = 0;
}
else
{
stopgame = 1;
}
}
void BallThrow()
{
randomnum();
int BallChance;
int PokeRun;
BallChance = rand() % 2;
pokeball = pokeball - 1;
if (BallChance == 1)
{
printf("Gotcha!\n");
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to continue your journey!\n\n");
poke_captured = poke_captured + 1;
CaughtPokemon();
getchar();
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
}
else if (BallChance == 0)
{
PokeRun = rand() % 2;
printf("The pokemon broke free!!!\n");
if (PokeRun == 0)
{
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
else
{
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to throw pokeball!\n\n");
getchar();
BallThrow();
}
}
else if (PokeRun == 1)
{
printf("Oh no! The pokemon ran away!\n");
printf("Number of Pokeball left: %d\n", pokeball);
printf("Press ENTER to continue your journey!\n\n");
getchar();
if (pokeball == 0)
{
printf("Your pokeball has used up!\n");
printf("Press ENTER to check the summary of your journey\n");
getchar();
PokeSummary();
}
}
}
}
void CaughtPokemon()
{
if (rand_num <= 30)
{
PokemonCaught[0] = PokemonCaught[0] + 1;
}
else if (rand_num <= 50)
{
PokemonCaught[1] = PokemonCaught[1] + 1;
}
else if (rand_num <= 70)
{
PokemonCaught[2] = PokemonCaught[2] + 1;
}
else if (rand_num <= 85)
{
PokemonCaught[3] = PokemonCaught[3] + 1;
}
else if (rand_num <= 95)
{
PokemonCaught[4] = PokemonCaught[4] + 1;
}
}
void PokeSummary()
{
int point0, point1, point2, point3, point4, total;
point0 = (PokemonCaught[0]) * 10;
point1 = (PokemonCaught[1]) * 30;
point2 = (PokemonCaught[2]) * 30;
point3 = (PokemonCaught[3]) * 50;
point4 = (PokemonCaught[4]) * 70;
total = point0 + point1 + point2 + point3 + point4;
printf("You have successfully caught %d Pokemon!\n\n", poke_captured);
printf("You have caught:\n");
printf("Margikarp = %d (%dpoints)\n", PokemonCaught[0], point0);
printf("Charmander = %d (%dpoints)\n", PokemonCaught[1], point1);
printf("Jigglypuff = %d (%dpoints)\n", PokemonCaught[2], point2);
printf("Pikachu = %d (%dpoints)\n", PokemonCaught[3], point3);
printf("Dragonite = %d (%dpoints)\n", PokemonCaught[4], point4);
printf("\nTotal points = %d\n", total);
exitno();
}
char exitno()
{
char stay;
printf("Press 'y' to continue, press any other key to quit.");
scanf(" %c", &stay);
if (stay == 'y' || stay == 'Y')
{
clear();
return (main);
}
else
{
printf("Thank you for playing!!");
exit(0);
}
}
int clear()
{
total = total * 0;
poke_captured = poke_captured * 0;
PokemonCaught[0] = PokemonCaught[0] * 0;
PokemonCaught[1] = PokemonCaught[1] * 0;
PokemonCaught[2] = PokemonCaught[2] * 0;
PokemonCaught[3] = PokemonCaught[3] * 0;
PokemonCaught[4] = PokemonCaught[4] * 0;
pokeball = pokeball + 20;
}
I appreciate any help and i know my codes are far from being decent (sigh)
. Thanks
You have made an error when calling main in char exitno() function:
char exitno()
{
char stay;
printf("Press 'y' to continue, press any other key to quit.");
scanf(" %c", &stay);
if (stay == 'y' || stay == 'Y')
{
clear();
return ((char)main()); // main it should be called using parenthesis
}
else
{
printf("Thank you for playing!!");
exit(0);
}
}
That is not inside of your do-while loop.
If you want it to execute every time, then just move it down into the the loop. Otherwise, your program will move past in the first few milliseconds of operation. Alternatively, modify it (likely, put it within a new function) to print out some information that might change during execution of your program--say, the current number of pokeballs remaining.

Resources