Note that there are some explanatory texts on larger screens.

plurals
  1. POgarbage character at end of string?
    primarykey
    data
    text
    <p>Hi there I'm reading a string and breaking each word and sorting it into name email and phone number. with the string <code>joe bloggs joeblog@live.com 12345</code>. But once i break everything down, the individual separated variables which hold the name,email and phone number have garbage characters at the end of them. I cant figure out why.</p> <p>test file</p> <pre><code>//test file #include &lt;iostream&gt; #include &lt;string&gt; #include "iofunc.h" using namespace std; int main(){ string str1 = "joe bloggs joeblog@live.com 12345"; iofunc func; cout&lt;&lt;"|-----------------------getname DEMONSTRATION------------------|\n" &lt;&lt; endl; func.getName(str1); cout&lt;&lt;"the names are: " &lt;&lt; func.glob_name &lt;&lt; endl; cout&lt;&lt;"\n|-----------------------getphone DEMONSTRATION------------------|\n" &lt;&lt; endl; func.getPhone(str1); cout&lt;&lt;"the phone number is:" &lt;&lt; func.glob_phone &lt;&lt; endl; cout&lt;&lt;"\n|-----------------------getemail DEMONSTRATION------------------|\n" &lt;&lt; endl; func.getEmail(str1); cout&lt;&lt;"the email address is:" &lt;&lt; func.glob_email &lt;&lt; endl; return 0; } </code></pre> <p>here's my get name function, the class is too big to scroll through:)</p> <pre><code>void iofunc::getName(string arg){ lineProcess(arg); //make sure to call this depending on what function u are using int name_count = 0; int wspace_count = 0; int arg_len = arg.length(); //int char_len = 0; char name_temp[80]; name_count = numberofNames(); //line process was called before so this will work, //make sure you call line process before using this function //for special, condition when there is no space in front of names if (special_condition == true){ int i = 0; while(i &lt; arg_len){ name_temp[i] = arg[i]; i++; } glob_name = string(name_temp); } if (special_condition == false){ if (name_count == 1){ int i = 0; while (arg[i] != ' '){ name_temp[i] = arg[i]; i++; } glob_name = string(name_temp); } //for 2 names if (name_count == 2){ for (int i = 0; i &lt; arg_len;i++){ if (arg[i] == ' '){ wspace_count++; } if (wspace_count !=2){ name_temp[i] = arg[i]; } } glob_name = string(name_temp); } //for 3 names if (name_count == 3){ for (int i = 0; i &lt; arg_len;i++){ if (arg[i] == ' '){ wspace_count++; } if (wspace_count !=3){ name_temp[i] = arg[i]; } } glob_name = string(name_temp); } } } </code></pre> <p>basic jist of all that is, im using the function called lineProcess to figure out whether there is an email, phone and name in the argument string, And the numberofNames functions gives how many names there are so that I can act accordingly.</p> <p>I had to use <code>char name_temp</code> to copy just the names from string so that I can extract just that and assign it to the <code>string</code> variable named <code>glob_name</code>. It copies everything i need but it gives me that garbage after each extracted string.</p> <p>any idea?.</p> <p><strong>EDITED</strong></p> <pre><code>void iofunc::getName(string arg){ lineProcess(arg); //make sure to call this depending on what function u are using int name_count = 0; int wspace_count = 0; int arg_len = arg.length(); //int char_len = 0; char name_temp[80]; int index_track = 0; name_count = numberofNames(); //line process was called before so this will work, //make sure you call line process before using this function //for special, condition when there is no space in front of names if (special_condition == true){ int i = 0; while(i &lt; arg_len){ name_temp[i] = arg[i]; index_track = i; i++; } name_temp[index_track+1] = '\0'; glob_name = string(name_temp); } if (special_condition == false){ if (name_count == 1){ int i = 0; while (arg[i] != ' '){ name_temp[i] = arg[i]; index_track = i; i++; } name_temp[index_track+1] = '\0'; glob_name = string(name_temp); } //for 2 names if (name_count == 2){ for (int i = 0; i &lt; arg_len;i++){ if (arg[i] == ' '){ wspace_count++; } if (wspace_count !=2){ name_temp[i] = arg[i]; index_track = i; } } name_temp[index_track+1] = '\0'; glob_name = string(name_temp); } //for 3 names if (name_count == 3){ for (int i = 0; i &lt; arg_len;i++){ if (arg[i] == ' '){ wspace_count++; } if (wspace_count !=3){ name_temp[i] = arg[i]; index_track = i; } } name_temp[index_track+1] = '\0'; glob_name = string(name_temp); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload