GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2008, 12:50
valembe valembe is offline
New Member
 
Join Date: Oct 2008
Posts: 6
valembe is on a distinguished road

char * to float/double


Hi Guys still a bit rusty on c++ but thanx to u!!

CPP / C++ / C Code:
string d;
char v[1024];
char *ptr;
while(getline( file, d ))
{
	strcpy(v,d.c_str());
	ptr=strtok(v,",");
	x1=0;

         while (ptr !=NULL)
	   {
		if(y<x)
		{
		   float u;
		   [b]u = (float)atof(ptr);[/b]
	           N[x1].push_back(u);
		   x1++;
	         }
	     ptr = strtok(NULL,",");
	     y++;
	   }
        y=0;

		
}
My problem is arising on converting ptr to float or double. A typical line is like this: 75, 5.1, 0.23, 1.1 and im failing to retain any digit after the comma instead im getting : 75, 5, 0, 1 Any help is appreciated.
Last edited by LuciWiz : 01-Nov-2008 at 13:09. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-Nov-2008, 14:04
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: char * to float/double


- the problem is you have no header lines
....#include <iostream>
....using namespace std;
- the problem is there is no int main()
- the problem is you used tabs
- the problem is you had no closing bracket after y=0
Ok now what happens :
081031_char_to_f-1.cpp:9: error: ‘file’ was not declared in this scope
081031_char_to_f-1.cpp:13: error: ‘x1’ was not declared in this scope
081031_char_to_f-1.cpp:16: error: ‘y’ was not declared in this scope
081031_char_to_f-1.cpp:16: error: ‘x’ was not declared in this scope
081031_char_to_f-1.cpp:20: error: ‘N’ was not declared in this scope
081031_char_to_f-1.cpp:24: error: ‘y’ was not declared in this scope
cplusplus.com/doc/tutorial/ why do I bother...
  #3  
Old 01-Nov-2008, 14:34
valembe valembe is offline
New Member
 
Join Date: Oct 2008
Posts: 6
valembe is on a distinguished road

Re: char * to float/double


Quote:
Originally Posted by Howard_L
- the problem is you have no header lines
....#include <iostream>
....using namespace std;
- the problem is there is no int main()
- the problem is you used tabs
- the problem is you had no closing bracket after y=0
Ok now what happens :
081031_char_to_f-1.cpp:9: error: ‘file’ was not declared in this scope
081031_char_to_f-1.cpp:13: error: ‘x1’ was not declared in this scope
081031_char_to_f-1.cpp:16: error: ‘y’ was not declared in this scope
081031_char_to_f-1.cpp:16: error: ‘x’ was not declared in this scope
081031_char_to_f-1.cpp:20: error: ‘N’ was not declared in this scope
081031_char_to_f-1.cpp:24: error: ‘y’ was not declared in this scope
http://www.cplusplus.com/doc/tutorial/ why do I bother...

i jus copied the bit i was having problems with i have all headers and all its just that its part of a long program. My apologies Howard for the incomplete program! i managed
  #4  
Old 01-Nov-2008, 15:33
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: char * to float/double


I tested your code with minor modifications to make it run and I cant find any issues with it. The float values are coming out fine.
I haven't made any major changes.

input file used
------
75, 5.1, 0.23, 1.1
6.01, 7.44, 8.34, 9.05
------

CPP / C++ / C Code:
#include <iostream>
using namespace std;

#include<string>
#include<fstream>

int main() {
	char v[1024];
	char *ptr;
	float u;

	string d;
	ifstream file ("test1");
	if (file.is_open())
	{
		while (! file.eof() )
		{
			getline (file,d);
			strcpy(v,d.c_str());
			cout << endl;
			ptr=strtok(v,",");
			while (ptr !=NULL)
			{
				u = (float)atof(ptr);
				cout << ptr <<"\t";
				ptr = strtok(NULL,",");

			}
		}
		file.close();
	}

	else cout << "Unable to open file";

	cout << endl;
	return 0;
}
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

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
Pointer initialization causing program abend? emanresu C Programming Language 0 12-Dec-2006 11:36
lvalue compile error emanresu C Programming Language 7 16-Nov-2006 11:22
getting an error while compiling and running using different IDE. jaro C Programming Language 0 25-Aug-2006 10:14
Memory cannot be read? dlare9 C Programming Language 3 16-Nov-2005 08:03
Debug Assertion Failed! dlare9 C Programming Language 3 14-Nov-2005 00:18

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

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


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