Sign in to follow this  
MSalley

Curses!!!

Recommended Posts

The following won't work. Anyone?
//storing values in a string w/for loop
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int get;//variable to be manipulated in for loop that gets input
    int amount;//# of letters user wants to store
    int show;//variable to be manipulated in  the for loop that shows
             //the user what they input
    char store[amount];//string that stores the user's input
    cout<<"How many letters do you want to store? ";
    cin>> amount;
    cin.get();
    cout<<"Enter the letters you want to store.";
    for(get=0; get<amount; get++)//first for loop; gets input
    {
        cout<<"\n>>";        
        cin.get(store[get]);//uses value of int get to say which 
                            //"cell" it needs to be stored in
    }
    cout<<"You entered ";
    for(show=0; show<amount; show++)
    {cout<<store[show]<<", ";}//returns input
    cin.ignore();
}
//The only thing I need now is to get it to work!


Thanks. Gonna' expand on something like this for that trivia game I was foolishly talkin' 'bout earlier. What is a vector? (idiot's terms, please)

Share this post


Link to post
Share on other sites
i'm guessing this either won't compile or is giving you wierd output. the reason is because you are creating an array of unknown size. If you were doing dynamic allocation that would be fine but here you are creating a fixed size array. The problem is that the variable 'amount' isn't known at compile time so the compiler doesn't know how much memory to allocate for your 'store' array. Move the cin >> amount line before the char store[amount] and it should be fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by Morpheus011
i'm guessing this either won't compile or is giving you wierd output. the reason is because you are creating an array of unknown size. If you were doing dynamic allocation that would be fine but here you are creating a fixed size array. The problem is that the variable 'amount' isn't known at compile time so the compiler doesn't know how much memory to allocate for your 'store' array. Move the cin >> amount line before the char store[amount] and it should be fine.


It won't work. See this .

EDIT: If you want to make an array whose size you specify at runtime you'd do something like this:

cin >> amount;
char* store = new char[amount];

//later on before store goes out of scope remember to call delete[]
delete[] store;


Then again, you probably want to use std::vector since it's really cool. I assume you don't know about it yet, so here's a link to some documentation and some brief examples. Vectors!

Share this post


Link to post
Share on other sites
It does kind of work, except that it tries to take input 2 characters at a time, only allowing the user to store half of what he/she wants to.

Share this post


Link to post
Share on other sites
It takes two charecters at a time because it reads the new line charecter you hit aftre the input of the charecter(most probably).It is not the best way to input the charecters one by one.If you give me an idea what you need this for I might be able to give an idea for a better way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this