Make loop stop by pressing dot - c

I need to write a program that asks you to enter numbers and when you press dot it will end the loop and display how many numbers are positive and how many are negative, like for example 4 5 -9 .
The program should show 2 numbers are positive and 1 is negative, but that isn't my problem. I'm stuck at the part where the loop should stop when I press dot, and bear in mind we are at the starter ages of programming so I can't be using fancy stuff.
As you can see here I tried giving a variable two types (char and float) but that doesn't work I think because it uses the value of the character instead of the numbers themselves.
int main()
{
float p, n;
char a;
n = 0;
p = 0;
do
{
printf("type a number");
scanf("%s&%f", &a);
if (a < 0)
{
n = n + 1;
}
else if (a > 0)
{
p = p + 1;
}
else
{
}
}
while (a != '.');
printf(" positive numbers are %2.0f \n negative numbers are %2.0f", p, n);
return 0;
}

Running your code through the popular online c compiler here:
http://www.tutorialspoint.com/compile_c_online.php
Your code ran as expected. What happens when you type "."?
EDIT: (Solution)
The problem was that when you input a negative number,
a = '-'
which has an integer value greater than zero, hence not triggering the negative case.
The solution then, is to check for the negative sign:
if (a == '-') {...}
Also, note that the '.' entered to terminate the loop is being counted as a positive number.

You should just read the input into a string and convert it to an integer if the first character isn't a '.'. Something like this:
printf("type a number\n");
scanf("%s", a);
if (a[0] == '.') {
break;
}
i = strtoul(a, NULL, 10);
Then you can see if i is positive or negative. Declare a as a char[10] or something.

Related

Why does my program print something before it ends when I press ctrl + D?

So I wrote a simple program that converts a decimal to binary, that only accepts positive whole numbers. So numbers like -2 and 1.1 would output "Sorry, that's not a positive whole number." It infinitely asks the user to input a number until the user presses ctrl + D. However when I tested it it prints out the "Sorry..." statement before it ends the program.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
void DecToBin(int userInput){
int binary[32];
int i = 0;
while (userInput > 0) {
binary[i] = userInput % 2;
userInput /= 2;
i++;
}
for (int j = i - 1; j >= 0; --j) {
printf("%d", binary[j]);
}
}
int main(void) {
double userDec;
int temp;
printf("Starting the Decimal to Binary Converter!\n\n");
while(!feof(stdin)) {
printf("Please enter a positive whole number (or EOF to quit): ");
scanf("%lf", &userDec);
temp = (int)(userDec);
if ((userDec > 0) && (temp / userDec == 1)) {
printf("\n\t%.0lf (base-10) is equivalent to ", userDec);
DecToBin(userDec);
printf(" (base-2)!\n\n");
}
else {
printf("\tSorry, that was not a positive whole number.\n");
}
}
printf("\n\tThank you for using the Decimal to Binary Generator.\n");
printf("Goodbye!\n\n");
return 0;
}
(All the tab and newlines are just how it's supposed to be formatted so don't pay attention to that)
So from what I'm understanding, my program reads ctrl + D as the else in my while loops. So, any idea why that is?
It seems like you think C-d would trigger some kind of break in the code. Like the keyword break. This is not true.
Read this post to see what's happening when you press C-d: https://stackoverflow.com/a/21365313/6699433
That does not cause anything special to happen in the C code. scanf will simply not read anything. After the scanf statement, the code will continue as usual, so the code WILL unconditionally enter the if statement.
This is also a pretty severe thing, because you'll be using userDec uninitialized. scanf returns the number of successful assignments, and you should always check the return value. So in your case you want this:
if(scanf("%lf", &userDec) != 1) { /* Handle error */ }
Because if scanf does not return 1, userDec is unassigned.
To achieve what you want, simply do this:
if(scanf("%lf", &userDec) != 1)
break;

Frequency digit counting

I'm trying to find the frequency of digit into given string,which contain digit and letters. When i run the same program i get different result, look like random output. where is the problem ?
int main() {
char num[1001];
int digit[10];
int j,i;
int count;
scanf("%s",num);
for(i=48;i<=57;i++)
{
count = 0;
for(j=0; num[j] != EOF;j++)
{
if(num[j] == i)
{
count++;
}
}
printf("%d ",count);
}
return 0;
}
You have missed an & in the line with scanf before num. Correct code should be:
scanf("%s", &num);
When you are scanning for the value, you need to provide the address of the variable. That is you let your program know where to put the value. &num points to the address of the variable num. But if you just write scanf("%s",num) you are providing value of the num variable, which you don't care at all. You'll be overwriting that value anyway.
At the end of the string, computer puts a null character \0 whose ASCII value is 0, to denote that this is the end of the string. Kind of like a '.' but for a computer. So you check until you find the null character in the for loop like: num[j] != '\0' [Not EOF]
I don't see any issue here, it seems output is not readable properly
so try printing correctly like
printf("%c=>%d ",i, count);
so that you can read what number how many times. if you find wrong frequency,
post the input for which you are getting wrong output.
It seems like you're trying to compare a char element with an integer type.
for example:
char num[7] = {"ABC123");
if (num[3] == 1)
{
printf("True\n");
}
else
{
printf("False\n");
}
return 0;
// This will return False, even though the element at index 3 is "1".
I've ran your code, and it seems like the output is the same given the same input.
Edit:
We wanted to compare a two digit number to a char data type - which is essentially a character - a one digit number/case.
When we take a number N and modulo by 10, we get the last digit, example:
48 % 10 = 8
To get the first digit we simply divide by 10, and take the quotient, example:
48 / 10 = 4 (remainder 8).
With this knowledge, we can compare the n-th char with first digit, and the n+1-th char with the last one (given that we only compare two digits, we'll stop at n+1-th).
Tip: a number char can be turned into a int using char = char - '0'
char num[1001];
int digit[10];
int j,i;
int count;
scanf("%s",num);
for(i=48;i<=57;i++)
{
count = 0;
for(j=0; num[j] != EOF;j++)
{
if(num[j] - '0' == i / 10 && num[j+1] - '0' == i % 10)
{
printf("%c", num[j] - '0');
count++;
}
}
printf("%d: %d \n",i, count);
}
printf("\n");
return 0;
// This code will print the digits and the frequency in a new line:
48: n times
49: n times
.
.
.
57: n times

How to allow user to exit while loop from terminal?

For example, if I want to write a code to average an unspecified number of numbers that the user enters, how can I make it so that the user can determine the number of numbers? ie. if the user wants to average just three numbers, he types them in one at a time, and then types in something to signal that this is it.
I wrote something like
while(i!=EOF){
printf("type in a number: \n");
scanf("%f",&i);
array[x]=i;
x++;
}
"and then some code to average the numbers in the array".
The idea was that if the user wants to signal that he finished entering numbers, he types in EOF and then the while loop will stop, however this isn't working. When I type in EOF at the terminal, it just writes "type in a number:" indefinitely.
scanf returns information in two different ways: in the variable i, and as its return value. The content of the variable i is the number that scanf reads, if it is able to return a number. The return value from scanf indicates whether it was able to read a number.
Your test i != EOF is fundamentally a type error: you're comparing the error indicator value EOF to a variable designed to hold a floating-point number. The compiler doesn't complain because that is accidentally valid C code: EOF is encoded as an integer value, and that value is converted to a floating-point value to perform the comparison. In fact, you'll notice that if you enter -1 at the prompt, the loop will terminate. -1 is the value of the EOF constant (on most implementations).
You should store the return value of scanf, and store it into a separate variable. If the return value is EOF, terminate the loop. If the return value is 1, you have successfully read a floating-point value.
If the return value is 0, the user typed something that couldn't be parsed. You need to handle this case appropriately: if you do nothing, the user's input is not discarded and your program will loop forever. Two choices that make sense are to discard one character, or the whole line (I'll do the latter).
double i;
double array[42];
int x = 0;
int r = 0;
while (r != EOF) {
printf("type in a number: \n");
r = scanf("%f", &i);
if (r == 1) {
/* Read a number successfully */
array[x] = i;
x++;
} else if (r == 0) {
printf("Invalid number, try again.\n");
scanf("%*[^\n]"); /* Discard all characters until the next newline */
}
}
You should also check that x doesn't overflow the bounds of the array. I am leaving this as an exercise.
I want to do it by typing in something that's not a number
Then get the input as a string, and exit if it cannot be converted to a number.
char buf[0x80];
do {
fgets(buf, sizeof(buf), stdin);
if (isdigit(buf[0])) {
array[x++] = strtod(buf);
}
} while(isdigit(buf[0]);
In case of no input scanf() does not set i to EOF but rather can return EOF. So you should analyze scanf() return code. By the way you can receive 0 as result which actually means there is no EOF but number cannot be read.
Here is example for you:
#include <stdio.h>
#define MAX_SIZE 5
int main()
{
int array[MAX_SIZE];
int x = 0;
int r = 0;
while (x < MAX_SIZE)
{
int i = 0;
printf("type in a number: \n");
r = scanf("%d",&i);
if (r == 0)
{
printf("ERROR!\n");
break;
}
if (r == EOF)
{
printf("EOF!\n");
break;
}
array[x]=i;
x++;
}
}
You cannot write 'EOF'.. since you are reading into a number...
EOF equals -1.. so if he enterd that, the loop would stop
You can test for the return value of the scanf function. It returns EOF on matching failure or encountering an EOF character.
printf("type in a number:" \n);
while(scanf("%f",&i)!=EOF){
array[x]=i;
x++;
printf("type in a number:" \n);
}

How do I force user to input a positive integer?

Force user to input a positive integer and put user in a loop till they do.
So I want everything including characters not allowed just over > 0
I tried the following:
while (i < 0) do {
printf("please input a number that's positive");
scanf("%d", i);
}
For positive integer use the following code
int i;
do
{
printf("please input a number that's positive");
scanf("%d", &i);
}while (i < 0);
The c language provides no error checking for user input. The user is expected to enter the correct data type. For instance, if a user entered a character when an integer value was expected, the program may enter an infinite loop or abort abnormally.
Both of these while functions manage the numbers, the int k is the set integer which can only be set below 20, the first while loop makes a statement that calls for another scan if the number is greater than 20
and the second loop prints a k*k box.
Hope this helps.
int main ( )
{
int i, j,k;
printf("Please enter Box size:\n\n");
scanf("%d",&k);
while(k>20){
printf("Please enter a value below 20\n\n");
scanf("%d"),&k;
}
while(k<=20)
{
for (i = 0; i < k; i++)
{
printf("\n");
for (j = 0; j < k; j++)
{
printf("#");
}
}
return 0;
}
}
I would do this: declare char term and int wrong = 0.
do {
printf("Enter a number: ");
fflush(stdin);
if (scanf("%d%c", &n, &term) != 2 || term != '\n' || n <= 0) {
printf("Only positive numbers.\n");
wrong = 1;
}
else {
wrong = 0;
//do something here if correct;
}
} while (wrong);
The code above detects invalid input if the user entered a mixture of characters and numbers, or negative numbers (and zero).
However, it doesn't detect if the user entered trailing zeros in front followed by valid digits eg. 001or 00000738. If anyone else could figure this out, please share below. Thanks! :)
Here is another alternative of a function which takes in a char str[20] (of say, maybe 20 elements), analyses the string to check for positive integers, and returns a 0 or 1 accordingly. Lastly, convert that string to an integer using atoi().
int checkPositiveIntegers(char str[]) {
char *ptr = str;
if (*ptr == '-' || *ptr == '0') //checks for negative numbers or zero
return 1;
else {
do {
if (isdigit(*ptr) == 0) { //checks for non-digit at ptr location; isdigit() returns 0 if non-digit
return 1;
break;
}
ptr++;
} while (*ptr != '\0' && *ptr != '\n');
return 0; //returns 0 if positive integer
}
}
So the function only accepts positive numbers from 1 to 9,999,999,999,999,999,999 (up to 19 digits if char str[] holds 20 elements).
However, if you converted the string back to int n = atoi(str);, the maximum value it could reach would be 2,147,483,647 since n is declared as a signed integer. Play around with different datatypes for exploration.
Hope this helps! :)

Dealing with unexpected input

Just a small query really, through the use of scanf, which in my case, will be scanning in X number of integers into variables, each integer separated by a space. Any hints/clues as to how to deal with input if when the integers are input, there are no spaces between them, for example, my input is such X X X X, but if XX X X was input, how could I deal with that within my scanf function?
Bearing in mind my scanf(%d %d %d %d"....)
Cheers.
I would read one value at a time with a counter and check whether a number is larger than 9, 99 or 999 to check for multiple digits. If you do, extract each digit with division and increase your counter for each digit.
You could check the return value of your scanf() to make sure it matches, and then validate that the values are between 0 and 9 after you receive them. Like so:
int vars[4];
if (scanf("%d %d %d %d", vars[0], vars[1], vars[2], vars[3]) != 4) {
// error
}
Then check each variable for being in range:
for (int i = 0; i < 4; i++) {
if (vars[i] < 0 || vars[i] > 9) {
// error
}
}
I'd just avoid scanf(). If each integer is just a single digit, something like the following would probably work:
int vars[4];
for (int i = 0; i < 4;) {
int c = getchar();
if (isdigit(c)) {
vars[i++] = c - '0';
} else if (!isspace(c)) {
// error
break;
}
}
The above does of course assume that the digits are '0' to '9' and have increasing, sequential values... and are each represented by a single char -- but those are probably safe assumptions.
While scanf reads after the enter button is pressed, it might be easier to read the line as a string and then try to analyze it. You can correct your input with backspace etc. on a fully featured terminal, so it's a bit more comfortable for user than getchar.
We look for single digits only, is that right?
Maybe something like:
char buffer[SOMECOUNT];
int digits[4];
int read, i;
scanf("%s", buffer);
for(int i = 0; i < strnlen(buffer, SOMECOUNT); ++i)
{
if( read >= 4 )
break;
if( isdigit(buffer[i]) )
{
digits[read] = buffer[i] - '0';
read++;
}
}
if ( read < 4 )
printf(error...);
Of course, this SOMECOUNT constant makes the solution a bit fragile for nasty input, so you may want to use the limit: scanf("%20s",buffer) or even construct the format string to include SOMECOUNT.

Resources