data validation

D

Default User

Phlip said:
When you reply with Google Groups, use Preview, then Edit, to get the
replied-to text. Then trim most of it out, leaving enough context that we
can tell to what you replied.


There's a more direct way. Click "show options" and use the Reply shown
in the expanded header. Saves several steps over your method (which
does seem to work, to my surprise).




Brian
 
G

gurumare

Ok, i was finally able to use strtol after looking it up and studying
its structure. my final code looks like this:
void main()
{
char name[20];
cin.getline(name, 20);

const char *strPtr = name;
char *end = NULL;

int val = strtol(strPtr, &end, 10);
cout << val;
getch();

}

this is just a test i set up to make sure i was doing it correctly. If
there is anything else i shoulkd know or change, please let me know,
thanks.
 
V

Victor Bazarov

Ok, i was finally able to use strtol after looking it up and studying
its structure. my final code looks like this:

void main()

int main()
{
char name[20];
cin.getline(name, 20);

const char *strPtr = name;
char *end = NULL;

int val = strtol(strPtr, &end, 10);
cout << val;
getch();

There is no standard function 'getch'. Did you mean 'getchar'?
}

this is just a test i set up to make sure i was doing it correctly. If
there is anything else i shoulkd know or change, please let me know,
thanks.

What do you have your 'end' for? You don't seem to be using the value
you get in it back from 'strtol' in any way...

V
 
G

gurumare

There is no standard function 'getch'. Did you mean 'getchar'?

I included those iostream, and i included conio.h which has the
function getch(); i use it as a quick way to pause my script

What do you have your 'end' for? You don't seem to be using the value
you get in it back from 'strtol' in any way...

i added code later that displayed my 'end' to see if it was working
correctly. i basically set up that code so that if i needed to see what
was there i can.
 
P

Paul Roebuck

Ok, i was finally able to use strtol after looking it up and studying
its structure. my final code looks like this:
void main()
{
char name[20];
cin.getline(name, 20);

const char *strPtr = name;
char *end = NULL;

int val = strtol(strPtr, &end, 10);
cout << val;
getch();

}

this is just a test i set up to make sure i was doing it correctly. If
there is anything else i shoulkd know or change, please let me know,
thanks.

#include <iostream>
#include <cstdlib>
#include <cerrno>


int atoi(const char* nptr)
{
char *endptr;

errno = 0;
int value = static_cast<int>(std::strtol(nptr, &endptr, 10));
if (*nptr == '\0' || *endptr != '\0')
throw std::invalid_argument(buf);
if (errno)
throw std::range_error(buf);

return value;
}


int main()
{
try
{
char buf[20];
std::cin.getline(name, sizeof(buf));
std::cout << atoi(buf) << std::endl;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}

return 0;
}
 

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,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top