Home > front end >  Scanning 2 chars in one line
Scanning 2 chars in one line

Time:11-25

I am new to C and I am trying to scan two char variables in one line.

I used #define _CRT_SECURE_NO_WARNINGS because I have to. This is the piece of code that annoys me: scanf_s("%c-%c", &x,1, &y,1);

When inputting 2 chars, it looks something like this ---> e-f or a-b Also, I need to lose scanf_s and make it into scanf only.

Another thing that is kinda weird to me is that after I input two chars and do actions later, I reset values of x and y to 0 and need to input them again. However, when I type them in, I get completely different values.

Here is the whole code that I wrote so far. Task: input 2 letters and print them in order specified in input e.g. a-c --> ABC or c-a --> cba

#include<stdio.h>
#define MAX 100

int main(){

    char niz[MAX];

    char kraj[MAX] = {0};

    int razlika;//difference 

    char x, y;
    int i;
    char trenutniznak; //current character
    int stoppolje=0;//stop field
    int temp = 0;
    while (1)
    {
        scanf_s("%c-%c", &x,1, &y,1);
    

        if (x == '.' || y == '.')
        {
            return 0;
        }
        else if (y > x)//od pocetka do kraja 
        {              //(from start to end )
            razlika = y - x   1;
            
            for (i = stoppolje 1, trenutniznak=x; i <= razlika; i  , trenutniznak  )
            {
                niz[i] = trenutniznak;
                temp = i;
            }
            stoppolje = temp; // polje na kojem je zadnji znak
                              //(the field where the last character is )
            x = 0;
            y = 0;
            razlika = 0;
        }
        else 
        {
            printf("\n");
            return 0;
        }
    }
}

CodePudding user response:

Try to understand and then implement in your code

char c;
scanf(" %c",&c); // a whitespace character in scanf() would ignore any number of whitespace characters left in the input stream
printf("%c",c);

you can go through %c conversion specifier

CodePudding user response:

Scanning 2 chars in one line

Simplify. Use fgets() to read a line of user input and convert that into a string.

Avoid scanf(). It does not well read a line and is difficult to use when input is unexpected.

char buf[80];
if (fgets(buf, sizeof buf, stdin)) { 

Now scan/parse the string into the two characters. Look for bad input.

  char extra;
  if (sscanf(buf, "%c-%c %c", &x, &y, &extra)  != 2) {
    fprintf(stderr, "invalid input <%s>\n", buf);
  } else {
    // Use x, y in some fashion
    printf("Good input <%c> <%c>\n", x, y);
  }
}
  

Sneak: If you must use scanf(), add a scanf(""); before the fgets() to use scanf(). It will not read anything.

CodePudding user response:

"Task: input 2 letters and print them in order specified in input e.g. a-c --> ABC or c-a --> cba"

If scanf() is not necessary, consider using fgets(), then the resulting string array, say char buf[4] = {0}; the the array elements buf[0], buf[1] and buf[2] will contain for example 'a', '-' and 'c'. (buf[3] will contain \0 character.) These are just numeric values that happen to be within the ASCII character range, but can also be logically manipulated as numbers, so just output the sequence of numbers using a format specifier that will guarantee they appear as ASCII:

int main(void)
{

    char buf[4] = {0};
    int c = 0;
    printf("Enter maximum of three characters: eg 'a-d'\n(or ctrl-c to exit.)\n");
    while(fgets(buf, sizeof buf, stdin))
    {
        //test input
        if((buf[0] < 'a') || (buf[0] > 'Z') || 
           (buf[2] < 'a') || (buf[2] > 'Z') || 
           (buf[1] != '-')
          )
        {
            printf("Invalid input.  Try again\n(or hit ctrl-c to exit.");
        }
        else
        {
            buf[strcspn(buf, "\n")] = 0;//remove newline
            if(buf[0] > buf[2])
            {
               for(char i = buf[0];i >= buf[2];i--)
               {
                   printf("%c", i);
               } 
               printf("\n");           
           }
           else if ((buf[0] < buf[2]))
           {
               for(char i = buf[0];i<= buf[2];i  )
               {
                   printf("%c", toupper(i));//note UPPER CASE as specified
               }
               printf("\n");
           }
           memset(buf, 0, sizeof buf);//reset buffer for next iteration.
           while((c = getchar()) != '\n' && c != EOF);//clear \n from stdin
           printf("\nEnter maximum of three characters: eg 'a-d'\n(or ctrl-c to exit.)\n");
        }
    }
}

CodePudding user response:

Well I managed somehow to do it, thank you everyone for answering my question :)

#include<stdio.h>
#define MAX 1000

int main()
{
char niz[MAX];
char kraj[MAX] = {0};
int razlika;
char x, y;
int i;
char trenutniznak; 
int stoppolje=-1;
int temp = 0;
while (1)
{
    scanf_s(" %c-%c", &x,1, &y,1);

    
    if (x == '.' || y == '.')
    {
        for (i = 0; i <= stoppolje; i  )
        {
            printf("%c", niz[i]);
        }
        return 0;
    }
    else if(x == '_' || y == '_')
    {
        niz[stoppolje   1] = ' ';
        stoppolje  = 1;
    }
    else if (y > x)//od pocetka do kraja
    {
        razlika = y - x   1;
        trenutniznak = x;
        for (i = stoppolje 1, trenutniznak; i < razlika stoppolje 1; i  , trenutniznak  )
        {
            niz[i] = trenutniznak;
            temp = i;
        }
        stoppolje = temp; // polje na kojem je zadnji znak
        x = 0;
        y = 0;
        razlika = 0;
    }//ako idemo po redosljedu abecednom
    else if(x>y)
    {
        razlika = x-y   1;
        trenutniznak = x;
        for (i = stoppolje 1, trenutniznak; i < razlika   stoppolje   1; i  , trenutniznak--)
        {
            niz[i] = trenutniznak;
            temp = i;
        }
        stoppolje = temp;
        x = 0;
        y = 0;
        razlika = 0;
    }
    else if (x == y)
    {
        razlika = 0;
        trenutniznak = x;
        for (i = stoppolje   1, trenutniznak; i < razlika   stoppolje   2; i  , trenutniznak  )
        {
            niz[i] = trenutniznak;
            temp = i;
        }
        stoppolje = temp;
        x = 0;
        y = 0;
        razlika = 0;
    }
     
}

}

  • Related