This is the code that I have. Currently the code prints out all the seats at once. I need it to print one seat and then restart to the user prompt. it can not assign the same seat and should show first class full when there are no more seats.
int main(int argc, char** argv) {
int firstClass[] = {1, 2, 3, 4}; //first class array
int firstLen = 4; // first class length
int userInput;
int i;
//user prompt
printf("Please type 1 for First Class\n");
printf("Please type 2 for Economy\n");
printf("Please type 0 to Quit\n");
printf("\n");
scanf("%d", &userInput); // scanning for user input
if(userInput = 1){
for(i = 0; i < 4; i++){
if(i < firstLen){
printf("Class: First Seat Number: %d\n", firstClass[i]);
}
else{
printf("First Class Full. Next flight is tomorrow.");
}
}
}
}
You can track which seats are taken with a variable. Each time a user selects 1 print out the seat number for that index, and then increment the index.
You'll then wrap this in a loop (for or while) and only exit the loop when the user enters 0. Make sure you define the variable used to track the current index outside of the loop.
Something like this:
int main(void)
{
const int first_class[] = {1, 2, 3, 4};
const size_t first_class_size = sizeof(first_class) / sizeof(first_class[0]);
size_t first_class_index = 0;
int user_input = 0;
do
{
print_menu();
scanf("%d", &user_input);
if (user_input == 1)
{
if (first_class_index < first_class_size)
{
printf("Here is your ticket information:\n");
printf("Seat Number: %d, Class: First\n\n", first_class[first_class_index++]);
continue;
}
printf("No first class seats are available.\n\n");
}
} while(user_input != 0);
}
Related
Having trouble getting this code to print 1 seat then loop back to user prompt. it should loop back until the seats are filled then display first class/ecoomy full. this is the code i have so far.
int main(int argc, char** argv) {
const int firstClass[] = {1, 2, 3, 4}; //first class array list
const int economyClass[] = {5, 6, 7, 8, 9, 10, 11, 12}; //economy array list
int firstLen = 4; // first class length
int economyLen = 8; //economy class length
int userInput;
int i;
//user prompt
printf("Please type 1 for First Class\n");
printf("Please type 2 for Economy\n");
printf("Please type 0 to Quit\n");
printf("\n");
scanf("%d", &userInput); // scanning for user input
if(userInput == 1){
for(i = 0; i < 4; i++){
if(i < firstLen){
printf("Class: First Seat Number: %d\n", firstClass[i]);
}
else{
printf("First Class is Full. Next flight is tomorrow.");
}
}
}
if(userInput == 2){
for (i = 0; i < 8; i++){
if(i < economyLen){
printf("Class: Economy Seat Number: %d\n", economyClass[i]);
}
else{
printf("Economy is Full. Next flight is tomorrow.");
}
}
}
}
As you want to take input from user as long as userInput != 0, so you need to take user input within a loop.
This loop will break when userInput will be 0.
Now about printing available seat from firstClass and economyClass, you can maintain 2 variables through which you keep track of last printed seat. Just like you maintain indexing while printing values of an array.
Here's a sample code. I've given some inline comments for better understanding.
int main(int argc, char** argv) {
const int firstClass[] = {1, 2, 3, 4}; //first class array list
const int economyClass[] = {5, 6, 7, 8, 9, 10, 11, 12}; //economy array list
int firstLen = 4; // first class length
int economyLen = 8; //economy class length
int firstClassSeatNumber = 0;
int economyClassSeatNumber = 0;
int userInput;
int i;
while(true) {
//user prompt
printf("Please type 1 for First Class\n");
printf("Please type 2 for Economy\n");
printf("Please type 0 to Quit\n");
printf("\n");
scanf("%d", &userInput); // scanning for user input
if (userInput == 0) {
break; // breaking the loop as we won't take further input from user
}
if(userInput == 1) {
if (firstClassSeatNumber < firstLen) {
// if we've any seat left in first class, then we'll print it
printf("Class: First Seat Number: %d\n", firstClass[firstClassSeatNumber]);
firstClassSeatNumber++; // moving to next seat of first class
}
else {
// don't have any seat left in first class
printf("First Class is Full. Next flight is tomorrow.");
}
}
else if(userInput == 2) {
if (economyClassSeatNumber < economyLen) {
// if we've any seat left in economy class, then we'll print it
printf("Class: Economy Seat Number: %d\n", economyClass[economyClassSeatNumber]);
economyClassSeatNumber++; // moving to next seat of economy class
}
else {
// don't have any seat left in economy class
printf("Economy is Full. Next flight is tomorrow.");
}
}
}
}
I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.
I'm writing this program in c where I need to re-prompt the user after an invalid input. I came to a solution only to discover that if the user enters another invalid input after the re-prompt then it continues. Can someone please show me a better solution to this? I'll show you what I had anyway:
#include <stdio.h>
#include <ctype.h>
main()
{
int ctr; // loop counter
int custID[10] = {1, 3, 5, 9, 10, // ID array
6, 4, 7, 8, 2};
double custBal[10] = {153.56, 1300.45, 684.45, 990.45, 45.54, // Balance array
1100.34, 594.45, 1340.45, 1000.00, 1134.00};
int IDsearch; // For interaction
int found = 0; // Search criteria
int inner, outer, tempID, didSwap; // For sorting the arrays
double tempBal;
char ans;
/* Firs step: Sort the arrays for efficiency */
for(outer = 0; outer < 9; outer++) // <9 and not <10, because 9 numbers will be bubble sorted
{ // the highest (10th) will remain at the bottom
didSwap = 0; // Turns one after the arrays sort
for(inner = outer; inner < 10; inner++)
{
if(custID[inner] < custID[outer]) // Ascending sort
{
tempID = custID[inner]; // Must include both,
tempBal = custBal[inner]; // otherwise the arrays wont be linked
custID[inner] = custID[outer];
custBal[inner] = custBal[outer];
custID[outer] = tempID;
custBal[outer] = tempBal;
didSwap = 1; // Flag that a swap took place
}
}
if(didSwap == 0)
{
break;
}
}
/* Second step: Interacting with the program */
printf("***Customer Balance Search***\n");
do
{
printf("Which ID number do you want to check?\n");
scanf(" %d", &IDsearch);
for(ctr = 0; ctr < 10; ctr++)
{
if(IDsearch == custID[ctr])
{
found = 1;
break;
}
}
if(found)
{
if(custBal[ctr] < 1000)
{
printf("\nCustomer #%d has a balance of $%.2f.\n", custID[ctr], custBal[ctr]);
printf("Credit is good!\n");
}
else
{
printf("\nCustomer #%d has a balance of %.2f.\n", custID[ctr], custBal[ctr]);
printf("Credit is bad! No more credit!\n");
}
}
else
{
printf("\nCustomer #%d was not found!\n", IDsearch);
printf("Please enter a correct ID number!\n\n");
continue;
}
printf("\nDo you want to search another ID number?\n");
printf("Enter (Y)es or (N)o\n");
scanf(" %c", &ans);
ans = toupper(ans);
}
while((found != 1) || (ans == 'Y' && ans != 'N'));
printf("\nExiting...\n\n");
return (0);
}
Please reset found
do {
found = 0;
// ...
at the start of the do-while loop. It is not enough to initialise it when defined.
I am trying to copy the array winner from my function 'enter', so that i am able to just output it on the 'previous' function. When picking the option for the previous option I have gotten nothing outputting. Its only the last function named 'previous' that is not working, but to produce the problem the majority of the code is needed.
#include <stdio.h>
#include <string.h>
char enter(char names[][20]);
void menu();
void previous(char winner[][8]);
int main()
{
char names[16][20];
int i;
printf("Please enter the names of the players:\n");
/*Making the user enter 16 times*/
for (i = 0; i < 16; i++)
{
scanf("%9s", &names[i]);
fflush(stdin);
}
/*Clearing Screen*/
system("cls");
menu(names);
return names[16][20];
}
void menu(char names[][20], char winner[][8])
{
int choice;
printf("Please select one of the following options:\n\n"
"Press 1 to enter game results\n"
"Press 2 to display the current round\n"
"Press 3 to display the players advancing to the next round\n"
"Press 4 to display the previous round\n"
"Press 5 to exit the program\n");
scanf("%d", &choice);
if(choice == 1)
{
enter(names);
}
system("cls");
if(choice == 3)
{
previous(winner);
}
}
char enter(char names[][20])
{
int result;
int score1;
int score2;
int p, c, j, l, i;
char winner[8][8];
system("cls");
for(i = 0; i < 8; i++)
{
printf("\n\n%s vs %s",names[i],names[i+8]);
score1 = 0;
score2 = 0;
for(j = 0; j < 5; j++)
{
printf("\n\nEnter game %d results, press 1 if %s won or"
" 2 if %s won :\n",(j+1), names[i], names[i+8]);
scanf("%d", &result);
if(result == 1)
{
score1++;
}
if(result == 2)
{
score2++;
}
printf("\n\n1Current score is %d-%d", score1, score2);
if(score1 == 3)
{
printf("\n\n%s adavances to the next round!",names[i]);
strncpy(winner[i], names[i], 10);
printf("\n\nPress Enter to Continue");
getch();
system("cls");
break;
}
if(score2 == 3)
{
printf("\n\n%s adavances to the next round!",names[i+8]);
strncpy(winner[i], names[i+8], 10);
printf("\n\nPress Enter to Continue");
getch();
system("cls");
break;
}
}
}
system("cls");
printf("The players advancing to the next round are:\n\n");
for(p = 0; p < 8; p++)
{
for(c = 0; c < 8; c++)
{
printf("%c",winner[p][c]);
}
printf("\n");
}
printf("\n\nPress Enter to Continue");
getch();
system("cls");
menu(names, winner);
return winner[8][8];
}
void previous(char winner[][8])
{
int i, j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
{
printf("%c",winner[i][j]);
}
printf("\n");
}
}
There is no data for the array winner in your program! At least not when you call it for the first time.
The signature for the menu function is:
void menu(char names[][20], char winner[][8]);
but you call it from main like this:
menu(names);
The winner parameter is missing. This shouldn't happen, but you have declared a prototype for this function, namely:
void menu();
Unfortunately, C treats the empty parens as meaning "whatever parameters you pass", not as function that takes no parameters. That means that your function call slips by. The fix is to provide the correct signature for the prototype and also to pass a suitable winner array from main.
Strangely, your enter function provides a local array winner. This array will always be a new array when you call enter. That's probably not what you want. As is, your program should have one names and one winner array. (You can pass these arrays around, but you should make sure that tese arrays are consistent. Don't create new arrays when you really want to operate on existing ones.)
You also call your menu recursively. That means the you go ever deeper into the call structure without real benefit. Dont do that; use a loop instead: do display the menu while the user hasn't chosen "quit". (There are applications for recursive functions, but this isn't one.)
I'm having trouble figuring out how to go about making a program for reserving study rooms.
1. Am I going in the right direction as far as getting the user input for the menu?
2. Once the user selects an option, how can i assign their room selection a new value to appear as open or unavailable? (Pointers?)
#include <stdio.h>
int main(void)
{
int rm1 = 0;
int rm2 = 0;
int rm3 = 0;
int rm4 = 0;
int rm5 = 0;
int rm6 = 0;
int rm7 = 0;
int rm8 = 0;
int rm9 = 0;
int rm10 = 0;
int input;
while (input != 4)
{
printf("Welcome to the Study-Room-Reservation System.\n"); /* Asks for user entry */
printf("Please Select an Option.\n------------------------------------\n");
printf("1. Reserve study room.\n");
printf("2. Leave study room.\n");
printf("3. Check Availability.\n");
printf("4. Exit.\n");
scanf("%d", &input);
if (input == 1)
{
printf("Please enter room number you would like to reserve. \n");/* 1. Reserve a room */
scanf("%d" , &)
}
else if (input == 2)
{
printf("Please enter room number you would like to exit. \n"); /* 2. Exit a room */
}
else if (input == 3)
{
printf("The following rooms are available. \n"); /* 3. Show available rooms */
}
else
{
printf("Invalid Command.\n"); /* Invalid Command */
}
}
return 0;
}
instead of
int rm1 = 0;
int rm2 = 0;
int rm3 = 0;
.
.
use an array
int room[10]; //for 10 rooms
then use it in a way like,
if( room[i] == 0 )
//room vacant
else if( room[i] == 1 )
//room occupied