Thread: Array Help Please (user defined size)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Array Help Please (user defined size)

    I have having a bit of trouble, i need to write a program that converts fahrenheit to celsius (i can do the function for this part) but it needs to start with a loop that first prompts the user to "Enter Temperature:" and then stores the value entered into the array which is supposed to be no more than 50. I am having issues with how to input data into the array starting at 0 and keep going up 1 (all I can do is input data directly to myarray[50], but I want the user to input data and it be entered into the array like
    enter data one > put in array[0]
    enter data 2 > put in array[1]
    The program also has to ask the user if they want to keep entering data by pressing 1, or stop by pressing 0. The program then has to display how many arrays there are of temperatures (not the value of them, just how many there are). Please help!! This is what I have so far, any help greatly appreciated.

    insert
    Code:
    // Computer Lab test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    // call this function to convert celsius to fahrenheit (function name is convert)
    double 
    convert(double celsius){
       double fahrenheit = ((9.0/5.0) * celsius) + 32;
        return fahrenheit;
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	double myarray[50];
    	int flag;
    	flag =1;
    	while (flag==1)
    	{
    		// loop
    		cout << "Enter Temperature Reading:";
    		cin >>myarray[50]
    	;cout << " Enter 1 to input more values, 0 to stop";
    	cin >>flag;
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you know how to use a for loop? It's basically the same idea here. Loop from index = 0 and while index is less than 50, incrementing index each time. If the user enters 0 to stop, then break out of the loop.

    >> ;cout << " Enter 1 to input more values, 0 to stop";
    Did you get an error about a missing semicolon before that line? The semicolon should go at the end of the previous line. It works either way, but that is convention.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    //This declares an array of 50 doubles
    double myarray[50];
    
    //this is trying to store user input into the 51st element
    //You can only use myarray[0]...myarray[49] (= 50 doubles).
    cin >>myarray[50]

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    /this is trying to store user input into the 51st element
    //You can only use myarray[0]...myarray[49] (= 50 doubles).
    cin >>myarray[50]

    So for this part your saying each time I ask the user to input a Temperature, I have to put a cin statement for myarray[0], myarray[1], myarray[2] etc etc going up one each time until I reach 49? Is there any way to automatically put the user data into the array and tell it to go to the next one without inputing a number into [ ] ?


    >> ;cout << " Enter 1 to input more values, 0 to stop";
    in this line i got an error originally without the ; at the beginning, and visual studio c++ told me to enter it for some reason, it has done this a few times I think it is because i had the wrong type of brackets.

    >> Do you know how to use a for loop? It's basically the same idea here. Loop from index = 0 and while index is less than 50, incrementing index each time. If the user enters 0 to stop, then break out of the loop.


    I think I know how to set up a for loop for this array, and to have the user enter 0 to finish. My problems run into entering the value of what the user inputs into my array which is assumed to be no more than 50, and have them choose to enter 0 or 1 to continue and have it restart in the same loop. Then when they do choose to stop the loop it should print out not the value of the myarray[number] , but the number of actual arrays created by the user. Any help greatly appreciated thanks, im on a different computer atm or I would send my updated code.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    /this is trying to store user input into the 51st element
    //You can only use myarray[0]...myarray[49] (= 50 doubles).
    cin >>myarray[50]

    So for this part your saying each time I ask the user to input a Temperature, I have to put a cin statement for myarray[0], myarray[1], myarray[2] etc etc going up one each time until I reach 49? Is there any way to automatically put the user data into the array and tell it to go to the next one without inputing a number into [ ] ?
    You'd use a for loop . . .

    >> ;cout << " Enter 1 to input more values, 0 to stop";
    in this line i got an error originally without the ; at the beginning, and visual studio c++ told me to enter it for some reason, it has done this a few times I think it is because i had the wrong type of brackets.
    Look at your code.
    Code:
    		cin >>myarray[50]
    	;cout << " Enter 1 to input more values, 0 to stop";
    It would be more readable as
    Code:
    		cin >>myarray[50];
    	cout << " Enter 1 to input more values, 0 to stop";
    The missing semicolon errors are misleading, they always point to the following line (where the missing semicolon was detected).

    >> Do you know how to use a for loop? It's basically the same idea here. Loop from index = 0 and while index is less than 50, incrementing index each time. If the user enters 0 to stop, then break out of the loop.


    I think I know how to set up a for loop for this array, and to have the user enter 0 to finish. My problems run into entering the value of what the user inputs into my array which is assumed to be no more than 50, and have them choose to enter 0 or 1 to continue and have it restart in the same loop. Then when they do choose to stop the loop it should print out not the value of the myarray[number] , but the number of actual arrays created by the user. Any help greatly appreciated thanks, im on a different computer atm or I would send my updated code.
    Something like this should work:
    Code:
    do {
        get_a_number();
    } while(the_number is invalid);
    [edit] Sorry, I read the requirements as "the numbers entered must be less than 50", not "there must be less than 50 numbers entered." Ignore the last part of this post. See Daved's post below. [/edit]
    Last edited by dwks; 04-04-2007 at 02:39 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem is that you only need one loop, but you have two conditions to break the loop. You need to break the loop if you reach 50 (there is a maximum of 50 entries) and you need to break the loop if the user enters 0 instead of 1.

    There are different ways to solve this. You can put both conditions combined with && or || into a while control. You can also just loop based on one condition and use the break statement for the other.

    That is what I would do. Use a for loop to loop through all 50 potential array slots, and then check if the user enters 0 and break if they do. The break will exit the for loop right then.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    still need a bit of advice

    Ok, so thanks to the greatly appreciated help I have the program working somewhat correctly, I think. It has 2 loops, a for and while.. one allows the user to keep inputting data into an array that will be defined by the user in size but is no more than 50 values. I have a while loop in this that will ask user to input 1 to continue, 0 to stop entering temperatures. My problem is, I think the values are being stored correctly cause when i choose to output say myarray[0] the correct value I input comes up, but I want to be able to print out how many actual arrays there are in a statement like "Number of readings entered is 12," not the values in them (although I hope i stored each one properly).

    This is where I am stuck. From here I have to take the data and make sure its all in centigrade. Im pretty sure I can write a basic function to do this part, but how do I make a statement to call this in my loop as the user inputs functions, and then prints out how many there are? Any help much appreciated thanks.




    Code:
    // Computer Lab test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    double myarray[51];
    int i;
    int flag;
    flag=1;
    for(i= 0; i < 50; i++)
       while (flag==1)
    		{
    		// loop to enter temperatures - pressing 1 continues, pressing 0 stops //
    		cout << "Enter Temperature Reading:"; 
    		cin >>myarray[i]; 
    		cout << "Enter 1 to input more values, 0 to stop";
    		cin >>flag;
    		}
    	std::cout << myarray[i] << std::endl;
    
    		return 0;
    }

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is not going to help. You are not changing the place where you put user input in the while loop. If it did, it wouldn't check for the <50 limit.

    This has been suggested:
    Code:
        int flag = 1;
        for (int i = 0; i < 50 && flag == 1; i++) {...}

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    This is what I have now. it compiles but I don't know if I made the function correctly or how to use it within my loop to make the check and ensure all values entered are celsius. Thanks for the help.

    Code:
    // Computer Lab test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    double c;
    double celsius;
    double CheckCent(double c)
    {
        if ( c>30 )
        {
            return 1.8 * celsius + 32;
        }
    	return c;
    }
    
    
    // call this function to convert celsius to fahrenheit (function name is convert)
    int _tmain(int argc, _TCHAR* argv[])
    {
    double myarray[51];
    //double i;
    int flag;
    flag=1;
    int i;
    for(i = 0; i < 51 && flag == 1; i++)
    {   // Loop to enter temperatures - pressing 1 continues, pressing 0 stops //
    	cout << "Enter Temperature Reading:";
    	cin >>myarray[i];
    	cout << "Enter 1 to input more values, 0 to stop";
    	cin >>flag;
    }// End for
    cout << "Number of elements in the array is: " << i;
    cout <<"\n";
    
    		return 0;
    }

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    double myarray[51];
    //double i;
    int flag;
    flag=1;
    int i;
    for(i = 0; i < 51 && flag == 1; i++)
    You're allowing 51 numbers there. Maybe you only want to allow 50?

    Also, what is CheckCent() supposed to do? Remember that since you have a parameter passed to the function called c, if you want to access the global variable c, use
    Code:
    ::c
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of array
    By goran00 in forum C Programming
    Replies: 38
    Last Post: 04-02-2008, 09:57 AM
  2. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  3. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  4. array user definable size
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2004, 07:48 PM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM