0

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;
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • If the loop's condition is `i < numVals`, can you explain how you could possibly expect its `if`'s expression, "`i >= numVals`" to ever possibly be true? – Sam Varshavchik Sep 28 '22 at 02:16
  • 1
    `auto sep = ""; for(...) { cout << exchange(sep, "_") << numVals_2; }` – Eljay Sep 28 '22 at 02:19
  • `numVals_2` is a horrible name. It's not the number of values, it's an actual value. I also imagine that the output looks totally mangled as you're taking input at the same time that you're attempting to print your output. – sweenish Sep 28 '22 at 02:21
  • 1
    Related to [idiom-for-iterating-between-each-consecutive-pair-of-elements](https://stackoverflow.com/questions/35372784/idiom-for-iterating-between-each-consecutive-pair-of-elements). – Jarod42 Sep 28 '22 at 08:37

4 Answers4

1

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

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

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

In the corner case of numVals == 0, consider this prints one '\n', something I'd expect.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

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;
      //}
Lepo
  • 31
  • 2
0

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;
}
-1

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;
   }
    ```
Farid Fakhry
  • 354
  • 1
  • 10
  • VLAs are not a part of Standard C++, and should not be recommended. This answer is less condescending, but still kind of condescending, and for no reason. – sweenish Sep 28 '22 at 02:28
  • seeing his problem, i'm not sure if he knows about vectors, pointers or other means. I did however edit my code to have a MAX_SIZE value so it's const. – Farid Fakhry Sep 28 '22 at 02:29
  • That's not an excuse to recommend bad practices. And take your own free advice and use names that make sense; `nValues` is also a bad name. The macro for a constant is a bad practice, as is `using namespace std;`. – sweenish Sep 28 '22 at 02:34
  • lmao, nValues is a decent name to represent number of values. i dont think anyone would get confused by it. macro for constant is not really a bad practice, some people just don't like it. again, it looks like this dude is a student not a professional. let him know what is a namespace first – Farid Fakhry Sep 28 '22 at 02:38
  • You can choose not to take your own advice all day but giving a beginner bad code is never a good idea. For example, what happens when `nValue > MAX_ARRAY_SIZE`? `std::endl` is unnecessary as well. – sweenish Sep 28 '22 at 02:55