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:
- ~/ $ ./somamulti
- Type your name -> 1
- Segmentation fault
- ~/ $
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);
}
The function
isalpha
has as parameter a character (actually an int) and not a string. Check each character of your string.– anonimo