simple question

F

frankb.mail

Ok i'm new to C++ and am teaching myself i've hit a small block that I
can't seem to get around

I am trying to convert an array like std::string load[10]; into a
float array.

The code looks like this:

float flaod[10];

int u;
for(u=0; u < vertCount * 2; u++){

fload = atoi(load.c_str());

}

I keep getting an "undeclared identifier" error when I compile the code
can any one help me figure out why?
 
T

Thomas J. Gritzan

Ok i'm new to C++ and am teaching myself i've hit a small block that I
can't seem to get around

I am trying to convert an array like std::string load[10]; into a
float array.

The code looks like this:

float flaod[10];

int u;
for(u=0; u < vertCount * 2; u++){

fload = atoi(load.c_str());


atoi() converts to integer, not to float. And its an unsafe and old C-function.
Don't use atoi() in C++ code, instead use the technic in the FAQ:

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
}

I keep getting an "undeclared identifier" error when I compile the code
can any one help me figure out why?

What error exactly? On what line?

Read this on how to post:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
 
L

LR

Ok i'm new to C++ and am teaching myself i've hit a small block that I
can't seem to get around

I am trying to convert an array like std::string load[10]; into a
float array.

The code looks like this:

float flaod[10];

int u;
for(u=0; u < vertCount * 2; u++){

fload = atoi(load.c_str());

}

I keep getting an "undeclared identifier" error when I compile the code
can any one help me figure out why?


When I compile:

std::string load[10];
float flaod[10];
int u;
for(u=0; u < vertCount * 2; u++){
fload = atoi(load.c_str());
}
I get 15 errors from my compiler.

I doubt if this is really what your code looked like, so

int main() {
std::string load[10];
float flaod[10];
int u;
for(u=0; u < vertCount * 2; u++){
fload = atoi(load.c_str());
}
}
gets nine errors from my compiler, and

#include <string>
int main() {
std::string load[10];
float flaod[10];
int u;
for(u=0; u < vertCount * 2; u++){
fload = atoi(load.c_str());
}
}
gets two error messages, one of which is:
error: 'fload' : undeclared identifier

Which is something that I'm sure your compiler reports on too. Try to
be more specific in future. It really helps. But anyway, if I change
the code to:
#include <string>
int main() {
std::string load[10];
float flaod[10];
int u;
for(u=0; u < vertCount * 2; u++){
flaod = atoi(load.c_str());
}
}

I still get two errors.
error: 'vertCount' : undeclared identifier
warning: '=' : conversion from 'int' to 'float', possible loss of data

Because vertCount isn't part of the code you posted which makes me think
that perhaps you didn't post a complete code example that shows the
problem to begin with, and the warning, well. The warning is just bad.
What's in those strings anyway?

And I don't think that using atoi is a good idea. A std::istringstream
is probably a better idea. You can do some error checking that way.
And raw arrays might not be a good idea either.

So I rewrote your code.

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

template<typename T>
class V {
std::vector<T> v_;
public:
V() : v_() {}
V &a(const T &t) { v_.push_back(t); return *this; }
const std::vector<T> &v() const { return v_; }
};

int main() {
std::vector<std::string> load =
V<std::string>().a("23.5").a("52.6").a("7.3e-5").v();
std::vector<double> flaod(load.size());

for(unsigned int u=0; u<load.size(); u++) {
std::istringstream in(load);
in >> flaod;
// I like to check my work
std::cout << flaod << std::endl;
}

}

Please tell us what book you are reading. Or are you reading any books?
How are you learning?

LR
 
O

osmium

Ok i'm new to C++ and am teaching myself i've hit a small block that I
can't seem to get around

I am trying to convert an array like std::string load[10]; into a
float array.

The code looks like this:

float flaod[10];

int u;
for(u=0; u < vertCount * 2; u++){

fload = atoi(load.c_str());

}

I keep getting an "undeclared identifier" error when I compile the code
can any one help me figure out why?


flaod != fload

speling concistently is your freind.
 
D

David Harmon

On 31 Dec 2006 10:49:13 -0800 in comp.lang.c++, "(e-mail address removed)"
I keep getting an "undeclared identifier" error when I compile the code
can any one help me figure out why?

flaod != fload

Also, prefer double over float; use strtod()
 
M

Mari

atoi converts str to int and not to float

check here
http://msdn2.microsoft.com/en-us/library/y0ybw9fy(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/hc25t012(VS.80).aspx


osmium said:
Ok i'm new to C++ and am teaching myself i've hit a small block that I
can't seem to get around

I am trying to convert an array like std::string load[10]; into a
float array.

The code looks like this:

float flaod[10];

int u;
for(u=0; u < vertCount * 2; u++){

fload = atoi(load.c_str());

}

I keep getting an "undeclared identifier" error when I compile the code
can any one help me figure out why?


flaod != fload

speling concistently is your freind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top