Solution
#include <stdio.h>
#include <string.h>
int main()
{
char value[50];
char *end;
int sum = 0;
long conv;
while(conv != 0 )
{
printf("Enter a measurement and 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" );
return 0;
}
UPDATE Still having problems with new program..unsure where to go from here. It is accepting numbers and quotes, but it keeps multiplying any number I type in by 12 when it should only do that if I enter a single quote to specify feet. UPDATE2 Here are the functions for Assembly that we should keep in mind for writing the C program:
void printStr(char *)
Arguments:
edi = address of null-terminated string to print
Returns:
Nothing
void printUInt(unsigned)
Arguments:
edi = Unsigned integer to print
Returns:
Nothing
char getchar()
Arguments:
None
Returns:
eax = the next character
uinsigned readUInt()
Arguments:
None
Returns:
eax = an unsigned int read from stdin.
(eax is 0 on error)
I must write a C program which prompts the user to enter a measurement(number) and unit( ' or ", which represent feet or inches) and then prints out the Total length in inches AFTER the user enters a length of '0' which acts as a sentinel value.
This program is only needed to act as a way for me to then build an Assembly program with a similar structure. The thing is, it must be built around 4 functions that would be called to help perform some of the operations.
So my C program should be built similarly with those functions in mind. Here's what I have:
Here is my program:
UPDATED:
int main()
{
char value[50];
char *end;
int sum = 0;
long conv;
while(conv != 0)
{
printf("Enter a measurement and 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);
return 0;
}
OLD:
int main()
{
int sum = 0;
int value;
char symbol;
while(value != 1)
{
printf("Enter measurement and 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);
return 0;
}
I hope I have enough information here for someone to help me even though I know we're missing the complete functions for time being. And I know the IA32 assembly conversion is not my main question and I think I will save that for another if I get stuck, but I hope getting this High-Level language program corrected will get me going in the right direction. Thanks in advance!
Get the character using
%c
not%d
. Change the line into like this.While comparing you have to do like this,
Output after this,
When you think about writing a program that will be translated to assembly, you want to limit yourself to using calls that have a 1-to-1 correlation to the system calls available in assembly. That means leaving the high-level world of buffered input/output and using the unbuffered low-level input/output methods where you are responsible for counting each byte that goes in or out of the program. Even the low-level routines of
read
andwrite
in C, provide quite a few features that you will have to handle by hand in assembly.In order to provide a reasonable example of this low-level I/O, I've written a short example to take your input measurements in either feet or inches and keep a running total of the feet and inches entered. The code also converts any inches over 12 into feet accordingly. This example provides a good approximation of the approach you will take in assembly to do the same thing. Take time to understand what each part does, and let me know if you have questions. The lower-level code you write, the longer it becomes to accomplish seemingly simple tasks. Your assembly will probably be 3-times longer.
The program expects input of the form
numfeet'
ornuminches"
. So to enter 10 feet, enter10'
, for 10 inches, enter10"
:Example / Output
Update to your high-level code
If you are going to continue to use the high-level functions, then make use of the fact that
end
already points to the next character following the number after you callstrtol
. e.g.:I see several bugs in your "updated" code: one on nearly every line.
Header files missing. (It's much easier for us to help you if you give us a complete program that can be compiled and tested without modification.)
Should be
int main(void)
; empty argument parentheses are poor style.This is fine.
Use of uninitialized variable. I would write a do-while loop instead.
Unnecessary use of
printf
(betterfputs
since no formatting is required).If you have
getline
, use it.It is necessary to set
errno
to zero manually before any use of thestrto*
functions, because some errors are only visible that way.Your immediate problem is here. There are three bugs on this line:
strtol
returned an error.value[1]
is the second character read byfgets
, not the character immediately after the number. The character immediately after the number isend[0]
.==
, not=
; this condition is assigning'\''
tovalue[1]
and then testing whether'\''
is nonzero; since'\''
has to be nonzero, the body of the if-statement always executes, which is why you are seeing it always multiply by 12. This would have been more obvious if you had put spaces around the=
. If you had run the compiler with warnings enabled, it would have told you about this mistake.ok
Same problems as the earlier
if
-statement; should beelse if
.Missing
else
clause here in which you report an error because the character after the number was neither'
nor"
.Missing check for junk input after the number and unit specifier.
the rest is fine
You probably could also use a primer on how to check whether
strtol
returned an error. This is a little finicky, becausestrtol
has no special return value that indicates an error; if an error happened, it will either seterrno
, or not consume any characters. Unfortunately, when an error did not happen, it does not necessarily clearerrno
, so you have to do it yourself: