Home > Back-end >  I have a for loop that puts "_" in between numbers from an input, how do I make it so the
I have a for loop that puts "_" in between numbers from an input, how do I make it so the

Time:09-28

As I said above, I have a user input how many numbers there are, and then inputs the numbers they have. I know it is probably a simple fix, but can someone tell me what I'm doing wrong here.

Ex: If the input is 3 -55 55 -25, then the output is:

-55 _ 55 _ -25

#include <iostream>
using namespace std;

int main() {
   int numVals;
   int i;
   int numVals_2;
   
   cout >> "Enter how many numbers you have: ";
   cin >> numVals;
   
   for (i = 0; i < numVals;   i) {
      if (i >= numVals) {
         cout << numVals_2 << endl;
      }   
      else {
         cout << "Enter one of your numbers: ";
         cin >> numVals_2;
         cout << numVals_2 << " _ ";
      }   
   } 

   return 0;
}

CodePudding user response:

how do I make it so the "_" isn't in the end?

Perhaps change the separator?
First read the values in a prior separate loop into some array a[] and then print them out.

char *sep = "";
for (i = 0; i < numVals;   i) {
  cout << sep << a[i];
  sep = " _ ";
}
cout << endl;

CodePudding user response:

First,int the 'for' statement,i >= numVals will nerver happen,when i euqals numVals,C will exit the 'for' statement,so you should change your 'for' statement

   for (i = 0; i < numVals;   i) {

      //if (i >= numVals) {
      //   cout << numVals_2 << endl;
      //}   
      //else {
         cout << "Enter one of your numbers: ";
         cin >> numVals_2;
         /*it shows that this number is not the last number*/
         if(i   != numVals)
            cout << numVals_2 << " _ ";
         else
            cout << numVals_2;
      //}

CodePudding user response:

Thank you all for your comments! I really appreciate it. To those of you who are wondering, I am a noobie to C . I do realize that numValues_2 is a dumb name. I named it that way because this was a problem in a textbook that I am going through right now. I wanted a variable name that was quick and I didn't want to give it much thought. I know that was not the best practice and in the future, I will give more thought to my variable names so that they make more sense. I have posted the answer below if you guys are curious :)

#include <iostream>
using namespace std;

int main() {
   int numberValues;
   int i;
   int valueFromInput;
   
   cin >> numberValues;
   
   for (i = 1; i <= numberValues;   i) {
      cin >> valueFromInput;

      /*it shows that this number is not the last number*/
      if (i != numberValues) {
         cout << valueFromInput << " _ ";
      }   
      else {
         cout << valueFromInput << endl;
      }   
   }
   return 0;
}

CodePudding user response:

your program will print a letter followed by an underscore just after it's inputed.

Some free advicce:

  • always give names that reflects what your variable is.
  • in your for loop i will never be bigger or equal to i, this is the whole point.
  • you should not create i before the loop, create it with the loop saves on memory.

you should consider using arrays:

#include <iostream>
using namespace std;
#define MAX_ARRAY_SIZE 100
int main() {
    int nValues;

    cout << "how many numbers are gonna be inputed?" << endl;

    cin >> nValues;

    int values[MAX_ARRAY_SIZE];

    for (unsigned i =0; i< nValues;   i){
        cout << "enter the next number" <<endl;
        cin >> values[i];
    }

    for(unsigned i =0; i< nValues-1;   i){
        cout << values[i] <<"_";
    }
    cout << values[nvalues-1] <<endl;
   }
    ```
  • Related