Storing values from Edit Box into an array

This is a discussion on Storing values from Edit Box into an array within the C++ Programming forums, part of the General Programming Boards category; Hello I am a beginner in programming. Can anyone give me an idea on how to do this? I will ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    7

    Storing values from Edit Box into an array

    Hello I am a beginner in programming. Can anyone give me an idea on how to do this?

    I will be using an edit box to input space-separated numerical values. Then the input values will then be stored in an array of type double.

    For example:
    Input values to the edit box are: 1.1 2.2 3.3

    Input values will be stored in the array in the following manner:
    arrayNum[0] = 1.1
    arrayNum[1] = 2.2
    arrayNum[2] = 3.3

    Thanks.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Have you tried?
    Can you show us an example?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Here is my code. There is no error when I compile it but when I run it the program fails. Can somebody help? Thanks.

    Code:
    void CCStringParserDlg::OnOK() 
    {
    	// TODO: Add extra validation here
    
    UpdateData(TRUE);
    
    int i,j = 0; 
    int startIndex = 0;
    int endIndex = 0;
    int charCount = 0;
    double Values[4];
    CString strValues[4];
    
    // Loop to parse values from m_sValues CString variable and store to string array
    do
    { 
    	// Find position of space character in string contained in m_sValues CString variable
    	endIndex = m_sValues.Find(' ',startIndex);
    	charCount = endIndex - startIndex;
    	// Store captured value to array element of type string
    	strValues[i] = m_sValues.Mid(startIndex,charCount);
    	startIndex = endIndex + 1;
    	i++;
    }
    while (endIndex != -1);
    
    	// Store string array to double array
    	for (j = 0; j = 3; j++)
    	{
    		Values[j] = strtod(strValues[j],NULL);
    	}
    
    	// Display message containing first captured value
    	MessageBox(strValues[0]);
    
    	CDialog::OnOK();
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Code:
    for (j = 0; j = 3; j++)
    And what's this supposed to do, I wonder?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    I dont know if I get your question right but it basically the condition to terminate the for loop. It is 3 because the array has a maximum index value of 3.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Indeed, but the for-loop condition loops while the condition is true (NOT until the condition is true).
    And do you know the difference between "=" and "=="?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    I tried changing that part of code but still the program fails. The following contains my changes. So basically what I am trying to do in the code is to first parse and store the values in a string array using the do-while loop and then copy the values from the string array to the double array using the for loop.

    Code:
    if (endIndex = -1)
    {
    	// Store string array to double array
    	for (j = 0; j == 3; j++)
    	{
    		Values[j] = strtod(strValues[j],NULL);
    	}
    
    	// Display message containing first captured value
    	MessageBox(strValues[0]);
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Well, as I said, the condition is keep looping if true.
    Since j is initialized to 0 first, 0 != 3 and thus the condition is false and the loop will never execute.
    The for loop executes while the expression is true.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Now I get what your are trying to say. I already corrected my code. However my program still fails. I think what causes the failure is in the do-while part. I just can't figure out what it is.

    Code:
    if (endIndex = -1)
    {
    	// Store string array to double array
    	for (j = 0; j < 4; j++)
    	{
    		Values[j] = strtod(strValues[j],NULL);
    	}
    
    	// Display message containing first captured value
    	MessageBox(strValues[0]);
    }

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Another suggestion: Seeing asthe input is dynamic (not fixed), you could get a potential buffer overflow.
    Therefore, I do suggest you use std::vector. Ie:

    Code:
    std::vector<double> Values;
    std::vector<CString> strValues;
    Then use .push_back() to store your values and use .size() to query the size later in your loops.
    Also, what output DO you get?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    I was already able to fix it doing some changes. Here is the working code for parsing four numerical values in a CString variable separated by spaces. Thanks.


    Code:
    	int i = 0; 
    	int j = 0;
    	int startIndex = 0;
    	int endIndex = 0;
    	int charCount = 0;
    	double dValues[4];
    	CString strValues[4];
    
    	// Loop to parse values from m_sValues CString variable and store to string array
    	for (i=0;i< 4; i++)
    	{
    		// Find position of space character in string contained in m_sValues CString variable
    		endIndex = m_sValues.Find(' ',startIndex);
    			if (endIndex == -1)
    			{
    				// Store the last value to string array
    				charCount = m_sValues.GetLength() - startIndex;
    				strValues[i] = m_sValues.Right(charCount);
    				break;
    			}
    		charCount = endIndex - startIndex;
    		// Store captured value to array element of type string
    		strValues[i] = m_sValues.Mid(startIndex,charCount);
    		startIndex = endIndex + 1;
    	}
    
    
    		
    	// Store values from string array to double array
    	for (j = 0; j < 4; j++)
    	{
    		dValues[j] = strtod(strValues[j],NULL);
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 07:46 AM
  3. Multiline Edit Box Parser
    By The Brain in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2005, 06:15 PM
  4. Edit Box Questions PT. II
    By Quantrizi in forum Windows Programming
    Replies: 16
    Last Post: 08-12-2003, 10:42 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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