Thread: Short C++ Assignment

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Short C++ Assignment

    Hey all...

    I need to make a program to do the following...:

    1) prompt a user to enter a set of data, one at a time, and store these in an array, using "while" loop. The numbers must be of "float" type.
    2) Inform the user about the maximum number of data, and what to do when the last data point has been entered
    3) Write, to standard output, the data that were entered
    4) Write, again to standard output, a table containing the # of data points and the sum of data points.

    Here's what I have
    Code:
    #include <iostream>
    #include <stdio>
    
    main ()
    
    int numbers;
    std::cin >> number;
    int numbers[10];
    
    while(numberCount < 10)
    {
    ... I'm stuck!
    }
    
    std::cout << "Enter any letter if there is no more point to enter.";

    I know this is terribly incomplete, but I've never used C++ before and I'm googling my way about here... any help please?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Look into cin - it's how you get input from the user. If you know how to use a while loop, your array, cin and cout, you should just be able to follow the numbered instructions you listed. Just so you know - you're question is entirely too vague - please don't post and expect anyone to write the code for you. You really need to ask specific questions and show us that you've done your part.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    1
    1.What does the function oldModString() do?
    2. What are any issues or problems that you see with it?
    3. How could you improve the function, and what benefits would your improvements bring?
    4. What other additional improvements could you make to the rest of the code?

    plz help me with above questions.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Ok, I totally redid it. Here is what I have

    Code:
    #include<iostream.h>
    #include<math.h>
    main()
    {
    int i;
    float a[5],sum=0;
    cout<<"\n Enter 5 numbers:\n\t\t\t";
    for(i=0;i<5;i++)
    {
    if(i==4)
    {
    cout<<"\n You are entering the last number:\n\t\t\t";
    }
    cin>>a[i]; cout<<"\n\t\t\t";
    sum+=a[i];
    }
    
    {
    cout<<"\n The number of data: 5\t\t";
    cout<<"\n The sum is:\t\t"<<sum;
    cout<<"\n The average is:\t"<<(sum/5) ;
    
    }}
    I'm having the following troubles: I can't get the cout to print the entered data, and also, I wish to extend this program to enable any number of data to be entered, not just "5". This means I would have to signal the program to let it know when the last datum has been entered. Can you help me with this?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly. Furthermore, you used <iostream>, which is correct, but now you changed it to <iostream.h>, which is pre-standard and should be avoided. As far as I can tell, you do not need to include <math.h>, and if you did, it should be <cmath>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The main function must also return int. Any modern C++ compiler will refuse to compile the code unless every function has a proper return type.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM