Segmentation Fault in ! (Isalpha(string)) (C)

Asked

Viewed 26 times

0

My program is returning Segmentation fault when I enter a value that is not alpha, rather than falling into loop

do{
    name = get_string("Digite seu nome -> ");
    } while(!(isalpha(name)));

The function "get_string" functions as a scan, when, it prints "Type your name ->" and scans, sending the user input to the "name variable"

Output:

  1. ~/ $ ./somamulti
  2. Type your name -> 1
  3. Segmentation fault
  4. ~/ $

Follow below all the application code:

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>

int main(void){

    string name = "";

    do{
    name = get_string("Digite seu nome -> ");
    } while(!(isalpha(name)));

    float valor1 = get_float("Digite o primeiro valor -> ");
    float valor2 = get_float("Digite o segundo valor -> ");
    float soma = (valor1 + valor2);
    float multi = (valor1 * valor2);

    printf("%s, o valor de soma é: %f\ne o valor de multiplicação é: %f\n", name, soma, multi);
}
  • 1

    The function isalpha has as parameter a character (actually an int) and not a string. Check each character of your string.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.