954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help Would Be Appreciated

Anyone know how to accomplish this task on visual studio with C++?

Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the
range.
Notes:
• You do not know ahead of time how many numbers will be in the list.
• When you want to stop entering numbers, enter control‐Z.
• The range is the difference between the lowest and the highest number.
• The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

>>Anyone know how to accomplish this task on visual studio with C++?
Yes. Do you? Hint: start out the program very simply and prompt for input. When that is done put that in a loop. Compile and correct all errors again. After that, add code to implement the rest of the requirements.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

No. I definitely do not know how to do it. I will try my best with your given advice and post my results later. Thanks again Ancient Dragon!

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

>>No. I definitely do not know how to do it
That's why you are in school -- to learn how :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ok. I honestly have no idea where to begin. I seem to be having a mental block. Any help would be greatly appreciated.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Start here

#include <iostream>
using namespace std;

int main()
{
   // put your code here

}


Re-read my previous post #2 and code one sentence at a time. Code the line that displays the prompt. Get that to work and compile, then code another line that asks for user input.

Have you read your textbook and done the example programs at the end of each chapter? If not, then you should do that.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ok. I have been trying. I am unsure of how to prompt the user to give input without asking a question or entering a statement. This is the 17th assignment and I have been breezing through until now. Can someone please get me started so I can visually see what to do?

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

"to prompt" just smply means use cout to display the message cout << "Enter something\n"; and to get input for a string. You need toSTUDY YOUR TEXTBOOK for other variations of cin and cout.

std::string myname;
cout << "Enter your name\n";
getline(cin, myname);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

There is no textbook. There are some readings online for class but they are no help. We are given these assignments and are not even taught how to do them. Like I said, I have been having no trouble at all until now. We were not taught how to take infinite values until the user enters ctrl-Z. I really am trying hard to do this. I am completely stumped.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

>>There is no textbook.
What the hell :-O Are you doing this in some sort of school ? or learning c++ all on your own? I assumed you were in school, but maybe I was wrong.


What part(s) of the assignment don't you know how to do. cin or cout , or something else

I'm sorry, but we can't give you a complete course in c++. If you are on your own then I'd strongly suggest you spend some $$$ and buy a good c++ book that will teach you.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I am at a university. It is an intro class. I understand the basics but I don't understand this problem.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

>>I am at a university.

Quit. Go to some other university because that one obviously will teach you nothing.

Do you know what the mean is and how to calculate it? How about the range? If not, then google for those terms.

And like I said before, take it one small step at a time. As someone once said "use the Divde And Conquor" method to solve the problem.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I know what mean and range are. I do not know how to do the problem and how to define these values based on repeated user input. Thanks for the help but I am at a dead end.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I thought you said you know the basics. But apparently you don't.

int main()
{
   float mean = 0;
   float range = 0;
   float num = 0;
   float sum = 0;
   int NumberInputs = 0;
   // infinite loop
   for(;;)
   {


   }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
Ok. I have been trying. I am unsure of how to prompt the user to give input without asking a question or entering a statement. This is the 17th assignment and I have been breezing through until now. Can someone please get me started so I can visually see what to do?

No one gives an assignment like this as the first assignment, so I assume you already know how to use cin and cout and store things in variables. I assume you had to do that in at least some of those sixteen earlier assignments. Also, no one assigns an assignment where you don't know how many elements there are going to be without first teaching you about arrays where you do know how many elements there are going to be, so I assume you know how to use arrays at least a little bit and you know how to use cin and cout, but you have not shown any code.I know what mean and range are. I do not know how to do the problem and how to define these values based on repeated user input. Thanks for the help but I am at a dead end.

Since you don't know how many elements there are going to be in advance, you need to either use an array that is bigger that the largest possible number of elements or you need to use a dynamic array or you need to use something like a vector. A vector would be the easiest in my opinion.

To start off, don't worry about calculating anything. Just give the user instructions, read in a value, push the value onto the vector, check whether the user is done, and if not, repeat. Then display the list. http://www.cplusplus.com/reference/stl/vector/

You'll be using the push_back function and the [] operator to read in and access the information. The size function will come in handy too. I don't think you'll need anything else from the vector class template. That should get you started. Give it a try, post some code, and go from there.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

>>and if not, repeat. Then display the list.
Displaying the list is not a requirement as the OP posted.

You could use an array, but its not necessary because there is no need to keep all the input values. Just sum them up as they are entered. You also need another counter to keep count of the number of entries the user entered. After all done, then just do the simple math.

This is really a very simple program that requires no (zero) advanced things such as , arrays or memory allocations. Don't make this more difficult than is needed.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>>and if not, repeat. Then display the list. Displaying the list is not a requirement as the OP posted.

You could use an array, but its not necessary because there is no need to keep all the input values. Just sum them up as they are entered. You also need another counter to keep count of the number of entries the user entered. After all done, then just do the simple math.

This is really a very simple program that requires no (zero) advanced things such as , arrays or memory allocations. Don't make this more difficult than is needed.

Yes, AD, you are correct. I didn't read the problem description closely. I thought all of the values had to be displayed, but they don't. To the OP, disregard my vector recommendation. AD is correct. No need to make it harder than it needs to be.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Step 1 - a loop which reads numbers and prints them out (this should be basically the same as a previous assignment)
Step 2 - make it exit on ctrl-z
Step 3 - make it reject out of range numbers
Got the idea yet?

Think about what steps you can
a) acomplish
b) might need to do first

When you've got somewhere (and got stuck), then post back.

Don't just stare at your "elephant" and complain that it's too big to eat all at once.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Here is my start. I want the user to be able to enter as many inputs as he wants but also be able to end them with ctrl-z. Right now, if I run it and enter '2', it prints 2, 2, press enter to continue.

#include <iostream>
using namespace std;

int main()
{
  double n;
  cin >> n;

  while (n>0) 
  {
    cout << n << endl;
	return 0;
}
}
joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Try re-examining the flow of your program. Think as the compiler: go line by line and ask "What is happening here?"

If you do not understand something in your code, I recommend you look it up on www.cplusplus.com

Evan M
Light Poster
42 posts since Sep 2007
Reputation Points: 11
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You