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. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include 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; }
-
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. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include 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; }
-
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. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include 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; }
planetx22 wrote:
cin >>myarray[50]
You're going to the same spot each time. Use a variable here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
You are off to a good start. I see what you need, but there is a practice around here not to do homework for students. So you tell me what it does so far, and more importantly what still needs to be done.
No problem, I understand and I hope I can learn through my errors. So far my program I think stores a temporary function at the start that converts any value of number entered into a double fahrenheit and returns it to another double celsius. I stored that there just for formula's sake, and haven't implemented it or really know how to use it yet. Then i think in this part I have correctly made an array of 50 doubles called myarray. Then i have made an int value flag that is in place of if the user puts a 1 or 0 to continue/stop the program. I want to be able to make an array that is assumed to be less than 50 values, and then the user to keep entering values until done, but I think I have it setup wrong is it just storing the values inputted into myarray[50] only? Do I have to seperatly do myarray[0], myarray[1], etc each time or is there some other loop i can make that can tell the program to keep entering values starting from [0] until when the user wants to stop. Then it should print out how many arrays there are rather than values in them. I just can't figure out how to stop the array and have the values only how many the user inputed, not all 50. Any help greatly appreciated thanks. // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include 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; }
-
planetx22 wrote:
cin >>myarray[50]
You're going to the same spot each time. Use a variable here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
if i make another variable, say i.. and define it like int i i=0 (not sure if this is right) for i=0, i<50, i++ { // cout "Enter Temperatures"; cin >> myarray[i] does that work better?
planetx22 wrote:
does that work better?
Probably, but you should surround your code snippet with <pre> tags and replace angle brackets with their non-translated version (see the buttons above the smileys).
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
planetx22 wrote:
does that work better?
Probably, but you should surround your code snippet with <pre> tags and replace angle brackets with their non-translated version (see the buttons above the smileys).
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
ok thank you. If i do use this and user myarray[i] as the user input, will this put data into 0-49 of the array , or until the user wants to stop?
planetx22 wrote:
...will this put data into 0-49 of the array...
Yes. Why not use one of the STL containers (e.g.,
vector
,list
) instead? That way, just enough room is allocated.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
planetx22 wrote:
...will this put data into 0-49 of the array...
Yes. Why not use one of the STL containers (e.g.,
vector
,list
) instead? That way, just enough room is allocated.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
haven't covered that much so we didn't have it mentioned as part of the project. I will look into them and post if I have any problems, thanks.
planetx22 wrote:
haven't covered that much so we didn't have it mentioned as part of the project.
That's fine. I found it better to know how to do it the "hard" way first. Then when I started using MFC (and some of STL), I understood it a lot better.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb