in Education by
I was supposed to do this exercise: Write a program in C to print a frequency chart on the screen. Input and output: The program receives as input a sequence of triples (number, frequency, character). For each sequence it should print a slash, producing a graph as in the example below. Example For input (5,12, -) (4,17, -) (2,1, -) (1,19, +) the program should print 5 | ------------ 12 4 | ----------------- 17 2 | - 1 | +++++++++++++++++++ 19 I realize that if I put a space before the sentence in the scanf function, it works very well, but the program desdon't end when it was expected int main() { int x, b, i, u; char n; do{ u = scanf(" (%d,%d,%c)", &x, &b, &n); printf("%d |", x); for (i = 0; i < b; i++){ printf("%c", n); } printf(" %d\n", b); }while(u == 3); } It was expected that, when the scanf don't read the 3 things it was supposed to, the while loop ends and the program is finished. But, when it happens, he still waiting for a new input. How do I fix that? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
just check for the returned value of u and then use an if condition. This shold stop the while loop and exit the program int main() { int x, b, i, u; char n; do{ u = scanf(" (%d,%d,%c)", &x, &b, &n); if(u == 3){ printf("%d |", x); for (i = 0; i < b; i++){ printf("%c", n); } printf(" %d\n", b); } }while(u == 3);

Related questions

0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    The while loop repeats a set of code while the condition is not met? (a) True (b) False I had ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    I have a program that loops through the lines of a book to match some tags I've created indicating ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
+1 vote
    ____ allows you to loop through a block of code as long as the specified condition is true. a)Tag b)While c)For...
asked Oct 8, 2020 in Technology by JackTerrance
0 votes
    I'm relatively new to C++, recently moved from C# and Java (and before that, used to work in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    If you have an existing package that doesn't have an .Rproj file, you can use devtools for the use_rstudio ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and ' ... #include "Fan.h" #include using namespace std; Fan::Fan() { cout...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and ' ... #include "Fan.h" #include using namespace std; Fan::Fan() { cout...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Write the syntax to set the path of the current working directory in R environment? (a) Setwd( dipath ) ... Debugging of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am working on a C++ program, which uses cURL library, which is written in plain C. When I try ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
...