Compiles but behaves wierdly - C++
Feb 9, 2015 at 3:11am UTC
I'm trying to write code for a function which would reformat code into a new format using a struct Entry[], and a sub array. Ultimately, the idea is to read the file in, but I can't figure out why this precursor isn't working
I need to read in as a string in format ("name 7-digitnumber") and turn it into ("name, 3digits-4digits"). The idea being to read many entries in this format, whereas this case is just one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
#include <string>
using namespace std;
// Starting Format = joe 2388789
// Ending format = joe, 238-8789
typedef char Name[20];
typedef char Phone[8];
typedef char HOLDING[100];
struct Entry { // Variable to hold all information
HOLDING temp;
Name name;
Phone phone;
};
void reformat ( Entry L[], int n ); // n is how many entries there are
int main (){
Entry L[50];
int num = 20;
L[1].temp[1] = 'j' ;
L[1].temp[2] = 'o' ;
L[1].temp[3] = 'h' ;
L[1].temp[4] = 'n' ;
L[1].temp[5] = ' ' ;
L[1].temp[6] = ' ' ;
L[1].temp[7] = ' ' ;
L[1].temp[8] = 9;
L[1].temp[9] = 1;
L[1].temp[10] = 1;
L[1].temp[11] = 9;
L[1].temp[12] = 1;
L[1].temp[13] = 1;
L[1].temp[14] = 1;
L[1].temp[15] = 9;
L[1].temp[16] = 9;
reformat(L, num);
return 0;
}
void reformat ( Entry L[], int n) {
for ( int i = 0; i <= n; i++) { // Initializing L[].phone and L[].name values
for ( int z = 0; z >=30; z++) {
if (L[i].temp[z] == '0' || L[i].temp[z] == '1' || L[i].temp[z] == '2' || L[i].temp[z] == '3' || L[i].temp[z] == '4' || L[i].temp[z] == '5' ) {
L[i].phone[z] = L[i].temp[z];
}
if (L[i].temp[z] == '6' || L[i].temp[z] == '7' || L[i].temp[z] == '8' || L[i].temp[z] == '9' ) {
L[i].phone[z] = L[i].temp[z];
}
else {
L[i].name[z] = L[i].temp[z];
}
}
cout << L[i].name << ", " << L[i].phone[0] << L[i].phone[1] << L[i].phone[2] << "-" << L[i].phone[3] << L[i].phone[4] << L[i].phone[5] << L[i].phone[6] << endl;
}
return ;
}
Feb 9, 2015 at 5:42am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <string>
#include <cstring>
std::string re_format( std::string name_space_dddeeee )
{
const auto sz = name_space_dddeeee.size() ;
if ( sz <= 9 ) return "string is too small" ;
auto name_end = sz - 9 ;
return name_space_dddeeee.substr(0,name_end) + // "name"
name_space_dddeeee.substr( name_end, 5 ) + '-' + // ", ddd-"
name_space_dddeeee.substr( name_end+5 ) ; // "eeee"
}
char * re_format( char cstr[], std::size_t n )
{
const std::string str = re_format(cstr) ;
return str.size() < n ? std::strcpy( cstr, str.c_str() ) : nullptr ;
}
int main()
{
char cstr[128] = "Andrew R. Koenig, 1234567" ;
if ( re_format( cstr, sizeof (cstr) ) ) std::cout << cstr <<'\n' ;
}
http://coliru.stacked-crooked.com/a/ae7d1f83fbde793e
Topic archived. No new replies allowed.