Yes, char* and char array are very often interchangeable. In fact, if you take a look at major_small's code above, he uses char*'s wherever I use a char array instead (with very minor changes) ;)
Printable View
Yes, char* and char array are very often interchangeable. In fact, if you take a look at major_small's code above, he uses char*'s wherever I use a char array instead (with very minor changes) ;)
>> you really have no need for a C++ string here (and in fact it might be harder to do it with one)
Decide for yourself: substr() http://www.cppreference.com/cppstrin...ls.html#substr
Yes, I know about substr() and c_str(), and they will work fine. But I personally like the C-style more than the C++ style in this case, if only because the C++ style requires an integer counter rather than directly pointing to the next block of memory... besides which, atoi() takes a c-style string (and yes I know about c_str() and stringstreams).Code:char* iterator = data;
strncpy(iterator, a, 2);
iterator += 2;
...
nA = atoi(a);
Code:int pos = 0;
a = data.substr(pos, 2);
pos += 2;
...
nA = atoi(a.c_str());
Ok, now I got how to divide them into different strings, but can anyone tell me how to convert them into intergers?
look up atoi and/or stringstream.
Someone already gave you the answer actually. Re-read the previous posts again, also in the cprogramming FAQ.
Yea, so I see. atoi is really simple, and does exactly what I want :D
Once again, thanks guys.
Sorry to ask for help so soon. I have tested the atoi translations, and they seem to work fine, yet all the whole program outputs is 000-00.
Code:
#include <vcl.h>
#include <iostream>
#include <stdlib>
#pragma hdrstop
using namespace std;
//---------------------------------------------------------------------------
#pragma argsused
int main()
{
int Iday, Imonth, Iyear, Ictrlnr, Igen, test;
char input[20], day[3], month[3], year[3], ctrlnr[4], gen[2];
char* i = input;
cout << "Indtast cprnr. i formatet xxxxxx-xxxx ";
cin >> input;
strncpy(i, day, 2);
i += 2;
strncpy(i, month, 2);
i += 2;
strncpy(i, year, 2);
i += 3;
strncpy(i, ctrlnr, 3);
i += 3;
strncpy(i, gen, 1);
Iday = atoi(day);
Imonth = atoi(month);
Iyear = atoi(year);
Ictrlnr = atoi(ctrlnr);
Igen = atoi(gen);
cout << Iday << Imonth << Iyear << "-" << Ictrlnr << Igen;
cin >> test;
return 0;
}
Actually it looks like the strncpy() lines are wrong. It should be
strncpy(month, i, 2);
etc.
And then it works :D
Thanks Hunter, you've been a great help.
No problem :)
I'm in trouble again.
I made the if statements, and tested them with cout's, but when I threw in the bool, it suddenly doesn't work. I have tried to look up bool, but I can't find anything that helps me out, and I can't see where it messes up. :(
It checks if the number is valid within the conditions given in the if files, and it worked. Then I added the bool variable, and the last part, and now the numbers don't work right. If I for ex. input 230378-4581, the integers return 232378-4581, when it should be 23378-3035.Code:bool valid = true;
int Iday, Imonth, Iyear, Ictrlnr, Igen, test;
char input[20], day[3], month[3], year[3], ctrlnr[4], gen[2];
char* i = input;
cout << "Indtast cprnr. i formatet xxxxxx-xxxx ";
cin >> input;
strncpy(day, i, 2);
i += 2;
strncpy(month, i, 2);
i += 2;
strncpy(year, i, 2);
i += 3;
strncpy(ctrlnr, i, 3);
i += 3;
strncpy(gen, i, 1);
Iday = atoi(day);
Imonth = atoi(month);
Iyear = atoi(year);
Ictrlnr = atoi(ctrlnr);
Igen = atoi(gen);
cout << Iday << Imonth << Iyear << "-" << Ictrlnr << Igen;
if(Imonth == 1 || Imonth == 3 || Imonth == 5 || Imonth == 7 || Imonth == 9 || Imonth == 11)
{
if(Iday > 31)
valid = false; //Here I used cout << " Invalid cprnr."
}
else if(Imonth == 4 || Imonth == 6 || Imonth == 8 || Imonth == 10 || Imonth == 12)
{
if(Iday > 30)
valid = false;
}
else if(Imonth == 2)
{
if(Iyear%4 == 0)
{
if(Iday > 29)
valid = false; //same
}
else if(!Iyear%4 == 0)
{
if(Iday > 28)
valid = false; //and so on
}
}
else if(Imonth > 12)
valid = false;
if(Iyear > 99)
valid = false;
if(Iday > 31)
valid = false;
if(valid == true) //added this for output.
cout << endl << input <<" is a valid cprnr. ";
else if(valid == false)
cout << endl << input << " is not a valid cprnr. ";
Not sure if this is the cause though, but still.Code:else if(!Iyear%4 == 0) // Problem here, take away the ! use != instead
{
if(Iday > 28)
valid = false; //and so on
}
Ok, nevermind. The bool just seems to mess everything up, so I used the int variable test instead, and it works as it's supposed to.