scanf function with list of numbers - c

The program is supposed to read in digits one after the other to see if they are consecutive numbers or not. The second digit seems to be scanning as 21 all the time for some reason. Could anyone show me where i am going wrong? Thanks.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=1, e=1,n, digit1, compared;
char y, z;
scanf("n=%d, digits=%c%c", &n, &y, &z);
digit1 = atoi(&y);
compared = atoi(&z);
if(digit1 == 0)
{
printf("False");
}
else if(compared == (digit1 + 1))
{
digit1++;
for(a = 1;a < (n - 1);a++)
{
scanf("%c", &z);
compared = atoi(&z);
if(compared == digit1 + 1)
{
e++;
digit1++;
}
}
if(e == (n - 1))
{
printf("True\n");
}
else
{
printf("False");
}
}
else if(compared == (digit1 - 1))
{
digit1--;
for(a = 1;a < (n - 1);a++)
{
scanf("%c", &z);
compared = atoi(&z);
if(compared == (digit1 - 1))
{
e++;
digit1--;
}
}
if(e == (n - 1))
{
printf("True\n");
}
else
{
printf("False");
}
}
return 0;
}

Related

How to print binary number as hexdecimal?

I have a binary number, and I need to print it as hexdecimal number. (its a function in a bigger project)
I made this function, but I was only able to print it in reverse.
this is the function:
int printOctalToHex (unsigned int octalNum) {
unsigned int binaryNum;
int temp;
int i = 1;
binaryNum = octalToBinary(octalNum);
while (1) {
temp = (binaryNum % 10000);
binaryNum = binaryNum / 10000;
if (temp == 0) {
printf("0");
}
if (temp == 1) {
printf("1");
}
if (temp == 10) {
printf("2");
}
if (temp == 11) {
printf("3");
}
if (temp == 100) {
printf("4");
}
if (temp == 101) {
printf("5");
}
if (temp == 110) {
printf("6");
}
if (temp == 111) {
printf("7");
}
if (temp == 1000) {
printf("8");
}
if (temp ==1001) {
printf("9");
}
if (temp == 1010) {
printf("A");
}
if (temp == 1011) {
printf("B");
}
if (temp == 1100) {
printf("C");
}
if (temp == 1101) {
printf("D");
}
if (temp == 1110) {
printf("E");
}
if (temp == 1111) {
printf("F");
}
if (binaryNum < 1) {
printf("\n");
break;
}
i++;
}
}
Can someone help me with that please? TY!
And if someone has a better way to do it I will be happy to see, because my way is very long.
You need just two functions, one of which is a recursive function:
int f_putchar(char c)
{
write(1, &c, 1);
return 1;
}
void f_hex(int nb)
{
char *hex = "0123456789abcdef";
if (nb < 16)
{
f_putchar(hex[nb]);
}
else
{
f_hex(nb / 16);
f_hex(nb % 16);
}
}

C function doesn't return string, though it should

I'm super new in C, trying to solve CS50's credit problem here.
So I wrote a function, that should check some parameters, and return a string, which I use in the main function to print an answer.
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <math.h>
int lunh(long n);
char* check(long nu);
int main(void)
{
long number = get_long("Number: ");
printf("%s", check(number));
}
int lunh(long n)
{
int length, step_one, num;
long tens;
step_one = 0;
length = floor(log10(labs(n))) + 1;
for (int i = length, powering = 1; i > length / 2; i--)
{
tens = pow(10, powering);
num = ((n / tens) % 10) * 2;
if (floor(log10(abs(num))) + 1 > 1)
{
while (num)
{
step_one += num % 10;
num /= 10;
}
step_one += num;
powering += 2;
}
else
{
step_one += num;
powering += 2;
}
}
for (int i = length, powering = 0; i > length / 2; i--)
{
tens = pow(10, powering);
num = ((n / tens) % 10);
step_one += num;
powering += 2;
}
if (step_one % 10 == 0)
{
return 1;
}
else
{
return 0;
}
}
char* check(long nu)
{
int l, first_two_digits, first_one;
l = floor(log10(labs(nu))) + 1;
first_one = nu / 1000;
first_two_digits = nu / 100;
char* answer = NULL;
if (l == 15)
{
if (first_two_digits == 34 || first_two_digits == 37)
{
if (lunh(nu) == 1)
{
answer = "AMEX";
}
else
{
answer = "INVALID";
}
}
}
else if (l == 13 || first_one == 4)
{
if (lunh(nu) == 1)
{
answer = "VISA";
}
else
{
answer = "INVALID";
}
}
else if (l == 16)
{
if (first_two_digits == 51 || first_two_digits == 52 || first_two_digits == 53 || first_two_digits == 54 || first_two_digits == 55)
{
if (lunh(nu) == 1)
{
answer = "MASTERCARD";
}
else
{
answer = "INVALID";
}
}
}
else
{
answer = "INVALID";
}
printf("%s", answer);
return answer;
}
Input: 4003600000000014
Expected output: "VISA"
Current output: nothing, after inputting the number, the program stops.
At least one problem is here: else if (l == 13 || first_one == 4). From the spec:
Visa uses 13- and 16-digit numbers.
Visa numbers all start with 4
That if test will produce VISA if length is 13 or card starts with 4. The sample input is a 16 digit number.

condition where two dots is typed it should print no?

First time posting here, having a problem with this code.
I want it to print no when there is more than 1 dot, for instance '2..5'.
Tried to put the following if statement:
if(num[i] == '..'){
printf("no \n);}
however with no success.
Im new to programming!
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *num = argv[1];
if (num[0] == '+' && strlen(num) >= 2 || num[0] == '-' && strlen(num) >= 2 || num[0] == '.' || (num[0] >= '0' && num[0] <= '9'))
{
for (int i = 1; i < strlen(num); i++) {
if (!(num[i] == '.' || (num[i] >= '0' && num[i] <= '9')) ) {
printf("no \n");
}
}
printf("yes \n");
} else {
printf("no \n");
}
}
}
Use this:
if(!(strcmp(num[i], "..")))
instead of
if(num[i] == '..')
Parsing numbers is not trivial. But the following works:
double parsenum(const char *num)
{
unsigned int i=0;
int neg= 1;
double result= 0.0;
int nFraction=1;
while (num[i]=='-' || num[i]=='+') {
neg= neg * (num[i]=='-'? -1 : 1);
i++;
}
while (num[i]) {
if (num[i]>='0' && num[i]<= '9') {
if (nFraction==1) {
result= result * 10 + (num[i]-'0');
}
else {
result= result + ((double)(num[i]-'0') / nFraction);
nFraction *= 10;
}
i++;
}
else if (num[i]=='.')
{
if (nFraction>1) {
printf("%s: no\n", num);
return 0.0;
}
nFraction *= 10;
i++;
}
else
{
printf("%s: no\n", num);
return 0.0;
}
}
result *= neg;
return result;
}
Test inputs:
printf("%f\n",parsenum("2..5"));
printf("%f\n",parsenum("-2.5"));
printf("%f\n",parsenum("--2.5"));
printf("%f\n",parsenum("2.5.6"));
printf("%f\n",parsenum("++2.555"));
printf("%f\n",parsenum("255.555"));

why is the output wrong from what i calculate on paper based on my code?

I've written this code for a class project:
#include<stdio.h>
int main()
{
int n = 0, i = 1, c = 0, d = 0, a = 0, b = 0, j = 1;
scanf("%d", &n);
while (i <= n)
{
b = a;
scanf("%d", &a);
if (a >= b)
{
i++;
}
if (a == b)
{
j++;
}
if (a < b)
{
i++;
break;
}
}
while ((i <= n) && (i != j + 2))
{
b = a;
scanf("\n%d", &a);
if (a < b)
{
i++;
}
if (a > b || a == b)
{
i++;
c = 1;
break;
}
}
if (c == 1)
{
while (i <= n)
{
scanf("\n%d", &a);
i++;
}
printf("No");
}
while (i <= n)
{
b = a;
scanf("\n%d", &a);
if (a <= b)
{
i++;
}
if (a > b);
{
i++;
break;
}
}
while (i <= n)
{
b = a;
scanf("\n%d", &a);
if (a > b)
{
i++;
}
if (a < b || a == b)
{
i++;
d = 1;
break;
}
}
if (d == 1)
{
while (i <= n)
{
scanf("\n%d", &a);
i++;
}
printf("No");
}
if (c != 1 && d != 1)
{
printf("Yes");
}
}
and I expect the output for this income:
6
4 4 3 2 3 2
to be "No". But instead, it's "Yes". I go through all of the steps in my code on paper, and the answer i get on my own is correct. But when i run the program it's not. Can someone please explain why?
Here you have an error in your code, the semicolon is ending the instruction
while (i <= n)
{
b = a;
scanf("\n%d", &a);
if (a <= b)
{
i++;
}
if (a > b); // remove this semicolon
{
i++;
break;
}
}
EDIT: Just for clarity, is ending the "if" instruction, it's like an empty "if" like:
if ( a > b ) { }
so the
i++;
break;
get executed as the next instructions

strcmp will not correctly evaluate in if statements [duplicate]

This question already has answers here:
strcmp on a line read with fgets
(6 answers)
Closed 7 years ago.
#include <stdio.h>
#include <math.h>
#include <string.h>
#define size 7
int computeN(char s1[])
{
int n=-1;
if(strcmp(s1, "black") == 0)
{
n = 0;
}
else if (strcmp(s1, "brown") == 0)
{
n = 10;
}
else if (strcmp(s1, "red") == 0)
{
n = 20;
}
else if (strcmp(s1, "orange") == 0)
{
n = 30;
}
else if (strcmp(s1, "yellow") == 0)
{
n = 40;
}
else if (strcmp(s1, "green") == 0)
{
n = 50;
}
else if (strcmp(s1, "blue") == 0)
{
n = 60;
}
else if (strcmp(s1, "violet") == 0)
{
n = 70;
}
else if (strcmp(s1, "grey") == 0)
{
n = 80;
}
else if (strcmp(s1, "white") == 0)
{
n = 90;
}
printf("%d\n", n);
return n;
}
int computeN2(char s2[])
{
int n1=-1;
if(strcmp(s2, "black") == 0)
{
n1 = 0;
}
else if (strcmp(s2, "brown") == 0)
{
n1 = 1;
}
else if (strcmp(s2, "red") == 0)
{
n1 = 2;
}
else if (strcmp(s2, "orange") == 0)
{
n1= 3;
}
else if (strcmp(s2, "yellow") == 0)
{
n1 = 4;
}
else if (strcmp(s2, "green") == 0)
{
n1 = 5;
}
else if (strcmp(s2, "blue") == 0)
{
n1 = 6;
}
else if (strcmp(s2, "violet") == 0)
{
n1 = 7;
}
else if (strcmp(s2, "grey") == 0)
{
n1 = 8;
}
else if (strcmp(s2, "white") == 0)
{
n1 = 9;
}
printf("%d\n", n1);
return n1;
}
int computeExponent(char s3[])
{
int exp=0;
if(strcmp(s3, "black") == 0)
{
exp = 1;
}
else if (strcmp(s3, "brown") == 0)
{
exp = 10;
}
else if (strcmp(s3, "red") == 0)
{
exp = 100;
}
else if (strcmp(s3, "orange") == 0)
{
exp = 1000;
}
else if (strcmp(s3, "yellow") == 0)
{
exp = 10000;
}
else if (strcmp(s3, "green") == 0)
{
exp = 100000;
}
else if (strcmp(s3, "blue") == 0)
{
exp = 1000000;
}
else if (strcmp(s3, "violet") == 0)
{
exp = 10000000;
}
else if (strcmp(s3, "gray") == 0)
{
exp = 100000000;
}
else if (strcmp(s3, "white") == 0)
{
exp = 1000000000;
}
printf("%d\n", exp);
return exp;
}
int computeResistance(int x, int y, int z)
{
int omega = ((x+y) * z);
return omega;
}
int main(void)
{
char color_codes[10][7] = {"black","brown","red","orange","yellow","green","blue","violet","gray","white"};
char s1[7], s2[7], s3[7];
int n, n1, choice;
printf("Enter the colors of the resistor's three bands, beginning with\n");
printf("the band nearest the end. Type the colors in lowercase letters\n");
printf("only, NO CAPS\n");
printf("Band 1 =>\n"); //prints prompts for bands
fgets(s1, size, stdin); //stores band 1 in s1
printf("Band 2 => \n"); //prints prompt
fgets(s2, size, stdin); //stores band 2 in s2
printf("Band 3 => \n"); //prints prompt
fgets(s3, size, stdin); //stores band 3 in s3
printf("Resistance value: %d\n", computeResistance(computeN(s1), computeN2(s2), computeExponent(s3))); //computes resistance
return (0); //make the exit
}
The strings are being stored properly; the issue is the fact that within the functions the correct value is not being found within the comparison in order to find the resistance with the algorithm. If the user enters 'red', 'red', 'red', the values n,n1, and exp will be equal to -1,-1,0. What could be causing this?
The problem here is, fgets() scans and stores the trailing newline, too, into the input buffer.
You need to get rid of that newline before sending the input for comparison. Otherwise, your string comparison is pretty much likely to fail.
A very simple solution can be like, check the string length of the input, then check on the last index value against the newline (\n), if you found that, replace that with null (\0) and then, pass on the input to the comparison function(s).
fgets includes the newline character in the string it reads. You'll either have to remove it or use a function like scanf which does not include the newline. (If you use scanf, remember to use e.g. %7s specifiers to prevent buffer overflows).

Resources