Thread: C++ arrays

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    C++ arrays

    Greetings forum members!
    My name is Steven. But I'm known as Tech2011 around various sites.
    I am new to this forum. Anyways, I have a quick question about C++ arrays.

    So I was hoping to design an array with each array value would increment to the next value.
    My code is displayed below. I will then explain my problem.

    cout << "Please enter an integer." << "\n" ;
    int userint;
    cin >> userint;
    cin.ignore();

    int arraybuildup[2];
    arraybuildup[0]=userint;
    arraybuildup[1]=userint++;


    cout << arraybuildup[1];

    I can't explain why, but for some reason, when a value is given for "userint", it outputs the same value on the screen, instead of the incremental value. This is not what I intended.
    If anyone has an idea or solution, your thoughts would be most certainly appreciated!
    Thanks for your support!
    Tech2011

    P.S Follow me on Google Plus. I'm Steven Styffe.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    userint++
    gets evaluated after the expression does.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    If only those Postfix increment operators had a meaningful name.....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah you probably want the prefix version, not the postfix one. (Put the ++ in front of the variable)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

Tags for this Thread