1

What I am trying to do, is allow the user to input any number of variables (for ex: 1 6 945 fhds)and my program will check for any strings. I have heard of arrays, but I feel like I can only limit how many inputs the user can have. Foo seems to need pre-input coded into the program? Can anybody clarify how to do this?

I tried:

#include <iostream>
using namespace std;

int main() {
int x;
int array[x];
cout << "Enter your numbers: ";
for (int x = 0; x = <char>; ++x) {
cin >> x;
}
cout << x; 
return 0;
}
NinaPortman
  • 29
  • 1
  • 2
  • Do you want to store the strings, or simply print them? – pushkin Oct 16 '15 at 04:05
  • This `int x; int array[x];` will bring you nothing but pain. For one thing it won't compile on all compilers because x is not a constant, but more importantly, x has not been assigned a value so if this does compile, the size of the array is undefined. – user4581301 Oct 16 '15 at 04:07
  • @user4581301 I want to respond to the string: if the input is numeric, do y, if not, do x. That's another problem I have, but I think I can only ask one question at a time. In this particular question, I wasn't sure how to actually input unlimited strings, and how to scan them in order. – NinaPortman Oct 16 '15 at 04:07
  • Based on your fuzzy question "I have heard of arrays" and your dodgey code snippet, I suggest you get a text book or do an online tutorial. – John3136 Oct 16 '15 at 04:07
  • @John3136 Can you give a link to an online tutorial? I looked online for array tutorials, and as I said in my post, the only allow a limited amount of inpit (ex: 5). In one particular tutorial, the author mentioned unlimited input, but they wrote something like int foo {43, 58,32, 4234}, meaning that the user cannot input anything really. – NinaPortman Oct 16 '15 at 04:12
  • [The big list of books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Oct 16 '15 at 04:13
  • I was wondering if there was any purely online material: I am unable to get a book right now actually. – NinaPortman Oct 16 '15 at 04:15
  • I can't recommend any entry level tutorials, I'm afraid. SO was my C++ tutorial porting over from C. [This site is extremely helpful](https://isocpp.org/wiki/faq), but assumes you're going in knowing at least sort of what you want. [cplusplus.com](http://www.cplusplus.com/) is pretty good at the basics. [cppreferrence](http://en.cppreference.com/w/) is more precise and complete than cplusplus, but again a higher level of expected knowledge. – user4581301 Oct 16 '15 at 05:01

4 Answers4

2

In C++ a resizeable array is called a "vector":

vector<int> array;
cout << "Enter your numbers: \n";

int temp;
while (cin >> temp)
    array.push_back(temp);

cout << array.size() << '\n';
M.M
  • 138,810
  • 21
  • 208
  • 365
1

C++ is not exactly a quick language to pick up. There are very many caveats and design patterns that you pretty much need to do a fair amount of study to become familiar enough to not do damage. "The big list of books" user4581301 suggested is a great start honestly.

There are some good references online as well, but even more bad ones.

There are several issues with your problem for example (besides the issues mentioned already with initialization and such).

I will try to address two off the top from a higher level.

  1. I would recommend resorting to arrays when you have to. Prefer a different abstraction when you can. Choose your data types wisely. Here are some standard containers for example. You could also utilize boost. You can use pointers and treat it like an array in which case you are in complete control of managing the memory. You can even use a hybrid approach and use a contiguous container like a vector and access it via a pointer. Pointers and arrays are fairly brittle, however, and error prone. In the face of exceptions they are even more so and I would recommend to always consider RAII principles first.

  2. Depending on the data you are planning to read you may need to consider your types you are using. You may also need to consider encoding. I would have a look at utf-8 everywhere for some insight into the world of strings.

Community
  • 1
  • 1
Matthew Sanders
  • 4,875
  • 26
  • 45
0

there are a handful of problems so far, like the multiple declarations and missuses of the variable 'x'; the for loop probably shouldn't be using x since it is already defined and x shouldn't be an array size when it is uninitialized.

as for what you want to get done it's kind of hard to tell but I think I can help.

int main()
{
    const int ARRAY_SIZE = 1000; //initialize so it doesn't end up being either 0 or literally anything
    int myArray[ ARRAY_SIZE ];
    for( int x = 0; x < ARRAY_SIZE; x++ )
    {
        cin >> myArray[ x ];
    }
    return 0;
}

now this will cycle through 1000 times asking for a numeral input until the array is written and full, if you want to be able to stop the array you'll have to add a way to break the for loop and record where it stopped.

int main()
{
    const int ARRAY_SIZE = 1000;
    int myArray[ ARRAY_SIZE ];

    int arrayEnd = 0;
    for( int x = 0; x < ARRAY_SIZE; x++ )
    {
        int inputBuffer = 0; // this variable saves the users input and checks to see if the user wants to exit before saving the variable to myArray.
        cin >> inputBuffer;
        if( inputBuffer != -1 ) // -1 is just a value the user enters to stop the loop, choose any number you want for this.
        {
            myArray[ x ] = inputBuffer;
        }
        else
        {
            arrayEnd = x;
            break; // stops the for loop if the number above is entered.
        }
    }

    if( arrayEnd == 0 )
    {
        arrayEnd = ARRAY_SIZE;
    }
    return 0;
}

if you want truely unlimited or a more malleable array of integers you can new an array of ints to set the size of the array like so

int main()
{
    int* myArray = nullptr;
    int arraySize = 0;
    cin >> arraySize;

    myArray = new int[ arraySize ];

    for( int x = 0; x < arraySize; x++ )
    {
        cin >> myArray[ x ];
    }

    delete[] myArray;
    return 0;
}

but I wouldn't recommend using new if you don't have most of the basics down since new can easily lead to memory leaks and more little things to keep track of.

  • While this sort of approach has some merit in straight C due to the amount of added complexity needed to eliminate its demerits, it really should be discouraged in C++ because it takes almost no extra effort to write code that isn't restrictive and prone to buffer overruns. –  Oct 16 '15 at 04:51
  • I'd disagree, while using arrays opposed to vectors like OP is you can only tell where an array ends by storing it as a value, which I do in every example; it is just a small thing to remember, like deleting new'd objects. – Garrett Hale Oct 16 '15 at 04:55
0
  1. Read the full line.
  2. Split by space and get the inputs
  3. Check the type of each input

.........

#include <string>
#include <iostream>
#include <vector>
#include <sstream>


std::vector<std::string> split(const std::string &s) {
    std::stringstream ss(s);
    std::string item;
    std::vector<std::string> elems;
    while (std::getline(ss, item, ' ')) {
        elems.push_back(item);
    }
    return elems;
}

int main()
{
    std::string line;
    std::getline(std::cin, line);
    std::vector<std::string> elems = split(line); 
    for (int i = 0; i < elems.size; i++)
    {
        /*
        if is number 
          do something
        else
            ignore
        */
    }   
}
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147