logo
down
shadow

Only first char of user input used in array


Only first char of user input used in array

By : 1ASW8W
Date : October 20 2020, 06:10 AM
around this issue Based on the comments, str is passed to stringin with the buffer uninitialized.
Since the following loop terminates based on the value of str[i] before str[i] is actually initialized, the results are undefined.
code :
for (i = 0; str[i] != '\n' && str[i] != '\033';) {
    str[i] = getch();
    ....


Share : facebook icon twitter icon
C - Sort char string in array to equal char user input

C - Sort char string in array to equal char user input


By : Bryan Lin
Date : March 29 2020, 07:55 AM
will be helpful for those in need I am having writer's block at the moment.
code :
#include <stdio.h>
#include <string.h>

void swap(char *a, char *b){
    char wk = *a;
    *a = *b;
    *b = wk;
}

int main(void){
    char wordInput[25];
    char newWord[25];
    int i, len;
    char *p;

    strcpy(wordInput, "poop");
    strcpy(newWord, "oopp");

    len = strlen( newWord );//assert(strlen(newWord)==strlen(wordInput))

    printf("newWold:%s\n",newWord);
    for(i = 0; i < len; ++i ){
        if(wordInput[i] == newWord[i])
            continue;
        if(NULL!=(p=strchr(&newWord[i+1], wordInput[i])))
            swap(&newWord[i], p);
        else
            break;
    }
    if(i < len){
        printf("can't...orz\n");
    } else {
        printf( "Back to original input: %s\n", newWord );
    }
    return 0;
}
strstr char array inside struct array with user input

strstr char array inside struct array with user input


By : fikrif
Date : March 29 2020, 07:55 AM
wish helps you fgets stores the \n and then stops taking input.
So suppose a player name is "user", players[i].name will be equal to "user\n" while a is "user".
code :
p = strstr(players[i].name,a);
while(fgets(line, 100, fp)!=NULL)
{
    players[i].no=i;
    strcpy(players[i].name, line);
    players[i].name[strlen(players[i].name)-1]='\0'; //add this line
    fprintf(stdout, "\nplayer=%s", players[i].name);
    players[i].age=20;
    i++;
} 
How to split user input into char array or string array and then check if palindrome or not in java

How to split user input into char array or string array and then check if palindrome or not in java


By : ktapp
Date : March 29 2020, 07:55 AM
wish helps you Your code will run fine if user inputs only a single word. But when you enter more than one word then till will give error. Example : input: racecar hello test your code will reverse this as : tset olleh racecar
racecar hello test!= tset olleh racecar
code :
String[] wordArray = wordGuess.split(",");
public checkPalindrome(String word){
    reverseWord="";
    for(int x=word.length-1;x>=0;x--){
        reverseWord = reverseWord+wordArray[x];

    }
    System.out.println(reverseWord);
    if(wordGuess.equalsIgnoreCase(reverseWord))
    {
        System.out.println("");
        System.out.println("You have found a Palindrome!!!");
        System.out.println("The Palindrome we found was "+reverseWord);
        break;
   }
   else{
        System.out.println("");
        System.out.println("You have not entered a Palindrome...");
        System.out.println("Please Try again...");

  }

} 
checkPalindrome(wordArray[0]);
checkPalindrome(wordArray[1]);
checkPalindrome(wordArray[2]);
WELCOME TO THE PALINDROME CHECKER!!!
Enter number of Strings with spaces to check palindrome
abba acac adda abaa
Pallindrome Strings
abba
adda
user input to a Char Array

user input to a Char Array


By : Santhosh Kumar
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Shouldn't userAnswers array be of size 10? Your program has quite redundant and unnecessary steps, according to me. So I have modified it to meet your need. There is no need to put the "Scanner in...." inside the loop.
This loop is full of mistakes. Not discouraging, just saying.
code :
     for (int i =0; i<uA;i++) 
     {
        Scanner in = new Scanner(System.in);//scanner inside loop
        System.out.print("Question #"+(i+1)+": ");
        userInput = in.nextLine();
        in.close();//already mentioned by someone in the comment
        int len = userInput.length();
        char[] userAnswers = new char [len];//no need to declare array inside loop
        for(int x =0;x<len;x++) 
        {
           userAnswers[i] = userInput.toUpperCase().charAt(i);
        }
  }

    System.out.println(userAnswers);//this will not print the array,it will print 
    //something like [I@8428 ,[ denotes 1D array,I integer,and the rest of it has 
     //some meaning too
char[] testAnswers = {'B','D','A','A','C','A','B','A','C','D'};
int uA =testAnswers.length;//to find the length of testAnswers array i.e. 10
char[] userAnswers = new char [uA]; 
char userInput;
int i;
Scanner in = new Scanner(System.in);
for (i =0; i<uA;i++) 
 {           
        System.out.print("Question #"+(i+1)+": ");
        userInput = Character.toUpperCase(in.next().charAt(0));
 }

  for(i=0;i<ua;i++) 
  {
       System.out.println(userAnswers[i]);
  }
  System.out.println("Data has been recorded");
cin.getline skips the reminder of the input if the user enters larger input than the char array

cin.getline skips the reminder of the input if the user enters larger input than the char array


By : TJ3238774
Date : March 29 2020, 07:55 AM
around this issue I just recalled the doc. of std::istream::getline():
code :
#include <iostream>

int main()
{
  const unsigned N = 3;
  const unsigned Len = 10;
  struct Student {
    char name[Len];
    int age;
  };
  Student students[N];
  for (unsigned i = 0; i < N; ++i) {
    std::cout << "Enter name of student " << i + 1 << ": ";
    std::cin.getline(students[i].name, Len);
    if (std::cin.fail()) {
      std::cerr << "Wrong input!\n";
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    std::cout <<"Enter age of student : " << i + 1 << " ";
    std::cin >> students[i].age;
    if (std::cin.fail()) {
      std::cerr << "Wrong input!\n";
      std::cin.clear();
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  }
  // check read
  std::cout << "\nStudents:\n";
  for (unsigned i = 0; i < N; ++i) {
    std::cout << i + 1 << ".: "
      << "name: '" << students[i].name << "'"
      << ", age: " << students[i].age << "\n";
  }
}
Enter name of student 1: Johann Sebastian
Wrong input!
Enter age of student 1: 23
Enter name of student 2: Fred
Enter age of student 2: 22
Enter name of student 2: Fritz
Enter age of student 2: 19

Students:
1.: name: 'Johann Se', age: 23
2.: name: 'Fred', age: 22
3.: name: 'Fritz', age: 19
Related Posts Related Posts :
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk