GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Jun-2005, 04:56
Vishy80 Vishy80 is offline
New Member
 
Join Date: Jun 2005
Posts: 3
Vishy80 is on a distinguished road

Converting Strings To Integer In Linked List


Does anybody know how I could get a string value from a file convert it to integer so I can do calculations and stuff, any help will be greatly appreicated

This is the code I got so far

CPP / C++ / C Code:
temp_totalprice1=atoi(Stock.RetailPrice);
temp_quantitysold1=atoi(temp_quantitysold);
temp_totalprice1=temp_totalprice1*temp_quantitysold;

On the above code it doesn't assign the value of (Stock.RetailPrice) to temp_totalprice
Last edited by LuciWiz : 05-Jun-2005 at 06:37. Reason: Pleae insert your C++ code between [c++] & [/c++]
  #2  
Old 05-Jun-2005, 07:19
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Vishy80
Does anybody know how I could get a string value from a file convert it to integer so I can do calculations and stuff, any help will be greatly appreicated

This is the code I got so far

CPP / C++ / C Code:
temp_totalprice1=atoi(Stock.RetailPrice);
temp_quantitysold1=atoi(temp_quantitysold);
temp_totalprice1=temp_totalprice1*temp_quantitysold;

On the above code it doesn't assign the value of (Stock.RetailPrice) to temp_totalprice

Hi Vishy80 and welcome to GIDForums™. How do you know that the value is not assigned? Are you using some quick printfs to see what the output is? Also, it appears to me that Stock.RetailPrice may want to be represented as a float and not an int, in which case the use of atof is needed.

Anyway, if you can give us a bit more info on this, we may be of more help.

Cheers,
d
  #3  
Old 05-Jun-2005, 22:12
Vishy80 Vishy80 is offline
New Member
 
Join Date: Jun 2005
Posts: 3
Vishy80 is on a distinguished road
Hi There
I actually did try the printf statement and it didn't return any value, anyways this is the rest of the code for that function

CPP / C++ / C Code:
 
void ProcessSales()
{
        char temp_quantitysold[5];
        char temp_totalprice[8];
        char temp_stock[8];
        char temp_reorderflag[2];
        int temp_totalprice1;
        int temp_quantitysold1;
        puts("\nEnter Stock Code\n");
	gets(SearchedStockCode);
	OpenStockFile();
        if(SearchStock())
	{
		puts("\nEnter Quantity Sold\n");
	        gets(temp_quantitysold);
                if (strcmp(temp_quantitysold,Stock.StockLevel)>0)
                {
                        puts("\nEnter Quantity Sold Is Incorrect\n");
                        return;
                }
                else
                {       temp_totalprice1=atoi(Stock.RetailPrice);
                        temp_quantitysold1=atoi(temp_quantitysold);
                        //temp_totalprice1=temp_totalprice1*temp_quantitysold;
                        puts("\nStock Code");
	                puts(Stock.StockCode);
	                puts("\nStock Description");
	                puts(Stock.StockDescription);
                        puts("\nQuantiy Sold");
                        puts(temp_quantitysold);
	                puts("\nTotal Price");
                        puts(temp_totalprice);
                        //gets(Stock.StockLevel);
                        if (strcmp(Stock.StockLevel,temp_quantitysold)>0)
                        {
                             strcpy(Stock.StockLevel, temp_quantitysold);
                             if (strcmp(Stock.ReorderLevel,Stock.StockLevel)>0)
                             {
                                if ((Stock.ReorderFlag)>0)
                                {
                                        temp_reorderflag=="Y";
                                        strcpy(Stock.ReorderFlag, temp_reorderflag);
                                }
                             }
                        }
                }
	}
	else
	{
		printf("Entered Stock Code Does Not Exist");
                CloseStockFile();
                fflush(stdin);
		return;
	}

}

When I initally save the stock file filed I do save it as a integer file rather than float
Cheers
Vish
Last edited by LuciWiz : 06-Jun-2005 at 00:25. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #4  
Old 06-Jun-2005, 04:14
aleppos's Avatar
aleppos aleppos is offline
New Member
 
Join Date: Jun 2005
Location: syria
Posts: 16
aleppos is on a distinguished road
hi,

maybe this code help you:

CPP / C++ / C Code:
bool string2int(char* digit, int& result) {
	if (digit == NULL) result = 0;
	else {
		result = 0;
		int minus = 0;
		while ((*digit >= '0' && *digit <='9') || *digit == '-') {
			if (*digit == '-') minus = 1;
			else result = (result * 10) + (*digit - '0');
			digit++;
		}
		if (minus == 1) result = result*(-1);
   		//--- Check that there were no non-digits at end.
		if (*digit != 0) {
			return false;
		}
	}
	return true;
}

int string2int(char* digit) {
	int result = 0;
	if (digit == NULL) result = 0;
	else{
		int minus = 0;
		while ((*digit >= '0' && *digit <='9') || *digit == '-') {
			if (*digit == '-') minus = 1;
			else result = (result * 10) + (*digit - '0');
			digit++;
		}
		if (minus == 1) result = result*(-1);
	//--- Check that there were no non-digits at end.
		if (*digit != 0) {
			return 0;
		}
	}
	return result;
}


I'm sorry if my information is incorrect/useless, I'm just trying to help :-) .
Good Luck - aLeppos
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
search linked list itsmecathys C++ Forum 20 18-Apr-2005 01:34
adding to linked list from external file cghv C Programming Language 3 09-Mar-2005 13:36
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 09:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:17.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.