linux - How to write C program that accepts user input that contain both integers and punctuation characters? -
linux - How to write C program that accepts user input that contain both integers and punctuation characters? -
solution
#include <stdio.h> #include <string.h> int main() { char value[50]; char *end; int sum = 0; long conv; while(conv != 0 ) { printf("enter measurement , unit(ex: 4' or 3\";0' or 0\" when done): "); fgets(value, 50, stdin); conv = strtol(value, &end, 10); if(strstr(value, "\'") != null) { conv = strtol(value, &end, 10); sum = sum + (conv*12); } else if(strstr(value, "\"") != null) { conv = strtol(value, &end, 10); sum = sum + conv; } } printf("total: %d, %s\n", sum, "inches" ); homecoming 0; }
update still having problems new program..unsure go here. accepting numbers , quotes, keeps multiplying number type in 12 when should if come in single quote specify feet. update2 here functions assembly should maintain in mind writing c program:
void printstr(char *) arguments: edi = address of null-terminated string print returns: nil void printuint(unsigned) arguments: edi = unsigned integer print returns: nil char getchar() arguments: none returns: eax = next character uinsigned readuint() arguments: none returns: eax = unsigned int read stdin. (eax 0 on error)
i must write c programme prompts user come in measurement(number) , unit( ' or ", represent feet or inches) , prints out total length in inches after user enters length of '0' acts sentinel value.
this programme needed deed way me build assembly programme similar structure. thing is, must built around 4 functions called help perform of operations.
so c programme should built functions in mind. here's have:
here program:
updated:
int main() { char value[50]; char *end; int sum = 0; long conv; while(conv != 0) { printf("enter measurement , unit: "); fgets(value, 50, stdin); conv = strtol(value, &end, 10); if(value[1]='\'') { sum = sum + (conv*12); } if(value[1]='\"') { sum = sum + conv; } } printf("total: %d\n", sum); homecoming 0; }
old:
int main() { int sum = 0; int value; char symbol; while(value != 1) { printf("enter measurement , unit: "); scanf("%d,%d", &value, &symbol); if(symbol == "'") { sum = sum + value*12; } else if(symbol == ''' ) { sum = sum + value; } sum = sum + value; } printf("total: %d", sum); homecoming 0; }
i hope have plenty info here help me though know we're missing finish functions time being. , know ia32 assembly conversion not main question , think save if stuck, hope getting high-level language programme corrected me going in right direction. in advance!
get character using %c
not %d
. alter line this.
if ( scanf("%d,%c", &value, &symbol) != 2 ) continue;
while comparing have this,
if(symbol == '\"') { sum = sum + value*12; } else if(symbol == '\'' ) { sum = sum + value; }
output after this,
enter measurement , unit: 2," come in measurement , unit: 3,' come in measurement , unit: 1," total: 45
c linux assembly
Comments
Post a Comment