modigy array elements..?

This is a discussion on modigy array elements..? within the C++ Programming forums, part of the General Programming Boards category; Hi, how can i modify an array's element. For example, i have initiliazed the array[0] to 0 and when the ...

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    modigy array elements..?

    Hi, how can i modify an array's element. For example, i have initiliazed the array[0] to 0 and when the user types something, then i want to become array[0]=1..and store the value.
    any help?
    Ready..Steady..Doom!!!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    >> Hi, how can i modify an array's element.
    You're pretty close....

    array[0] = 1;

    >> ...and when the user types something...
    You'll have to be more specific if you need help with that part. If it's just getting input from the user in general, the FAQ contains some code you can study.

    gg

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You could also do this
    Code:
    std::cin>>array[0];
    Woop?

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    Code:
    #include <iostream>
    
    int main() {
        char array[1];
        std::cout << "Enter whatever character you will and press enter: ";
        std::cin >> array[0];
        std::cout << "\nYou typed: " << array[0];
        return 0;
    }
    Of course it's not quite good since you may enter "abcdefghij...." and it will try to store it in array[0] wich will crash the program but as long as you type only 1 character and press enter it will work.

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Easy. Declare the array as an integer with ammount 0 as this:
    Code:
    int array[0]
    .
    Then set the value of the 1st place to equal 0 as this:
    Code:
    array[0]=0
    .
    Then ask for a user input using cin and a random variable, or cin.get(). After that set the 1st position of the array to equal 1 like this:
    Code:
    array[0]=1
    .

    Now array[0]==1.
    To code is divine

  6. #6
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    i want to make a loop to modify me array's elements..when the user types 1 i want the array[0] be 1; and restart the program, when a second user types 1 i want the array[1] be 1...(all values starts from 0).
    Ready..Steady..Doom!!!

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Try it out for yourself we showed you how and by re-start do you mean like close and exit and re-open or do you mean reset all variables to starting values
    Woop?

  8. #8
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    i want the modified values to be stored,when user types 1, and then go to starting menu without exit.. i can not figure a function to do that...
    Ready..Steady..Doom!!!

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    So you want to use a loop. You could then create temperary variables to hold the old values of the numbers
    Code:
    int main(void)
    {
       int a = 0;
       int tempA = 0;
       int change = 0;
       int exit = 0;
       
       while(exit == 0)
       {
           tempA = a;
           std::cout<<"A equals 0"<<std::endl;       
           std::cout<<"Enter a new value for a"<<std::endl;       
           std::cin>>a;
           std::cout<<"Is this value ok [0] Yes [1] No"<<std::endl;
           std::cin>>change;
    
           if(change == 1)
           {
    
               a = tempA;
    
           }
    
           std::cout<<"Enter 0 to continue any other number to quit"<<std::endl;
           std::cin>>exit;
       }
    
      return 0;
    
    }
    Woop?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Declare the array as an integer with ammount 0 as this
    That's illegal. C++ requires that array sizes be greater than zero.
    My best code is written with the delete key.

  11. #11
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by Prelude
    >Declare the array as an integer with ammount 0 as this
    That's illegal. C++ requires that array sizes be greater than zero.
    Ok I misworded it, I meant as 1 (which would be array[0]).
    To code is divine

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I meant as 1 (which would be array[0]).
    Your code was misworded too then:
    Code:
    int array[0]; // Illegal
    int array[1]; // Legal, but kind of silly because only array[0] is available
    My best code is written with the delete key.

  13. #13
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    thanks bman for the code..really help me!
    Ready..Steady..Doom!!!

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Of course it's not quite good since you may enter "abcdefghij...." and it will try to store it in array[0]
    No, it will not. It will only store the 'a' in array[0].
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    void messageFunction();
    void Titlos();
    int function(int);
    int main()
    {
    
    
    int a=0;
    
    int num;
    
    Titlos();
    cout<<endl;
    messageFunction();
    
    cout<<"Choose an option:"<<endl;
    cin>>num;
    
    while (num==1&&a<5)
    {
          a++;
    
          cout<<"You bought seat "<<a<<endl;
          cin>>a;
          break;
               
    }
    
    return main();
    
      system("PAUSE");	
      return 0;
    }
    
    void messageFunction(void)
    {
        cout<<"Type 1 for First class"<<endl;
    } 
    
    void Header()
    
    {
        cout<<"********Exercise 3 Solution********"<<endl;
    }
    could someone tell me how to save the a value when the 'while' statement starts again..?
    Ready..Steady..Doom!!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21