Reputation: 175
I need to write a program where the user inputs 3 items and their relative prices, on three separates strings. Then I have to check whether the last item and price are the same as one of the other two. If that is the case I have to print the item and the total price (the price of that item multiplied by 2).
The problem is that apparently when I call a function to divide the name of the item from the price (I would ideally save them as two separate strings, and then convert the float string of the price to a normal float variable), but it doesn't work.
This is the function
void f(char v[], char product[], char price[]){
int i=0, a=0, inword=0, z=0;
while (!z) {
if(isalpha(v[i])&&inword)
product[i]=v[i];
if (isalpha(v[i])&&!inword) {
inword++;
product[i]=v[i];
}
if (isspace(v[i])&&inword) {
inword=0;
a=1;
}
if ((isdigit(v[i])||v[i]=='.')&&a) {
price[i]=v[i];
}
if (a&&v[i]=='\n')
z=1;
i++;
}
puts(product);
puts(price);
}
I made sure it is properly defined in the main and before the main.
The input string should be something like this:
Apple 4.99\n
Therefore I first search for a letter, if I find one I start to save the word in the string product, then as soon as I find a space I exit from "inword" and I search for a digit (the price doesn't have € £ or $).
Ideally the outcome of the two strings would be:
product = Apple
price = 4.99
But if I try to print the price it's empty... Can you help me?
The side question is how do I search for £ $ € in the string? If I write '€' it gives me an error sign... Thank you for your help
Upvotes: 1
Views: 97
Reputation: 31409
You could do like this
void f(char v[], char product[], char price[]){
char * p = v;
while (!isdigit(p[0]))
p++;
memcpy(product, v, p-v);
product[p-v]='\0';
strcpy(price, p);
}
Upvotes: 1