Note that there are some explanatory texts on larger screens.

plurals
  1. POArray of strings converted into numbers and sorted
    primarykey
    data
    text
    <p>As a beginner I am trying to input one digit numbers in word form, but sort and display them as numbers. </p> <p>On inputting :</p> <p>seven three five one nil</p> <p>the output is</p> <p>0 1 3 5 7 How can i stop 0 from displaying and from being populated in num array in first place?</p> <pre><code>#include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; #define n 5 using namespace std; string words[n]; int nums[n],size; void input(){ cout&lt;&lt;"Enter in word form, the numbers to be sorted\n"; for( int i = 0; ; i++){ cin &gt;&gt; words[i]; if( words[i] == "nil" ) break; } size = sizeof words/sizeof(string); } void convert(){ for( int i = 0; words[i]!= "nil" ; i++ ){ if ( words[i] == "one" ) nums[i] = 1; // else cout&lt;&lt;"Wrong input\n"; } } void sort(){ sort(nums, nums + size); for ( int i = 0; i &lt; size; i++ ) cout&lt;&lt; nums[i]&lt;&lt;endl; } int main(){ input(); convert(); sort(); system("pause"); return 0; } </code></pre> <p>This works fine, I erred at length of words..to remove element nil (used as sentinel) from consideration of sort i had to reduce size by 1 . This code works just fine and the way it is intended to.</p> <pre><code> //headers and std string words[n]; int nums[n],size; void input(){ cout&lt;&lt;"Enter in word form, the numbers to be sorted\n"; for( int i = 0; ; i++){ cin &gt;&gt; words[i]; if ( words[i] == "nil" ) break; } size = sizeof words/sizeof(string) - 1;//one for nil } void convert(){ for( int i = 0; words[i]!= "nil" ; i++ ){ if ( words[i] == "one" ) nums[i] = 1; else if ( words[i] == "two") // } } void sort(){ sort(nums, nums + size); for ( int i = 0; i &lt; size; i++ ) cout&lt;&lt; nums[i]&lt;&lt;endl; } int main(){ // } </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