Need help with project

This is a discussion on Need help with project within the C++ Programming forums, part of the General Programming Boards category; Here is the project im working on, im having a hard time figuring out an algorithm that will read the ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    Need help with project

    Here is the project im working on, im having a hard time figuring out an algorithm that will read the minimum and maximum values. Also when i am confused about the end of input signal(cntl+Z). My instructor also tells me that the program should instantly print the output and that i dont have to hit enter once I use it. I think he is completely wrong about this but I am not sure. My program also keeps assigning ^Z a numerical value.






    Project description:

    Design and implement a C++ program that works as follows.

    First read a sequence of integer values. Stop reading values when the user enters the end-of-input signal, which is usually a <Ctrl-D> or <Ctrl-Z> combination on most computers.

    Then print the following statistics:

    * the total number of values that were entered
    * the number of times the value 0 appeared
    * the number of positive values
    * the number of negative values
    * the number of even values
    * the number of odd values
    * the minimum value
    * the maximum value
    * the sum of all the values
    * the average of all the values (as a floating point number)
    * the product of all the values (as a floating point number)

    Your program should correctly handle unusual cases such as when all the values are negative, or all the values are positive, or the total number of values entered is 0.

    The only library your C++ program may include is <iostream>.

    Here is a sample run. The user input is shown in boldface red.
    Enter value: 3
    Enter value: 6
    Enter value: 12
    Enter value: -2
    Enter value: -5
    Enter value: -8
    Enter value: 7
    Enter value: 1
    Enter value: 2
    Enter value: 5
    Enter value: <Ctrl-Z>

    Number of values = 10
    Number of zero values = 0
    Number of positive values = 7
    Number of negative values = 3
    Number of even values = 5
    Number of odd values = 5
    Minimum value = -8
    Maximum value = 12
    Sum = 21
    Average = 2.1
    Product = -1209600

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    My suggestion is to store the numbers in a dynamic array. Once you do that, finding the minimum and maximum values should be rather easy.

    Also when i am confused about the end of input signal(cntl+Z).
    In a way this allows you to simplify how you read the input values. You can use, say:
    Code:
    int num;
    while (std::cin >> num)
    {
    	// ...
    }
    Then expect the user to break out of the otherwise infinite input loop.

    My instructor also tells me that the program should instantly print the output and that i dont have to hit enter once I use it. I think he is completely wrong about this but I am not sure.
    I think your instructor is wrong too.

    Your program should correctly handle unusual cases such as when all the values are negative, or all the values are positive, or the total number of values entered is 0.
    Another thing you may want to consider is what if invalid input is entered, e.g. an alphabetic character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    sorry this was my effort so far



    Code:
    #include <iostream>
    using namespace std;
    void main()
    {
    	int input,int positive=0,int negative=0;
    	int odd=0,int even=0,int total_zeros=0,int total_numbers=0;
        
    
    
        while (!cin.eof()) 
    	{
        
        cout <<"enter value:\n";
    	cin >>input;
    
    	cout <<"enter value:\n";
    	cin >>input;
    	
        if ((input%2)==0)
        even ++;
    	else
    	odd ++;
    	
        if (input >= 0)
        positive ++;
    	else
    	negative ++;
    	
    	if (input ==0)
    	total_zeros ++;
    	else
    	total_numbers ++;
    
    	}
    	
    	cout <<"positive"<<positive<<endl;
    	cout <<"negative"<<negative<<endl;
    	cout <<"even"<<even<<endl;
    	cout <<"odd"<<odd<<endl;
        cout <<"total zeros"<<total_zeros<<endl;
        }

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    In a way this allows you to simplify how you read the input values. You can use, say:

    Code:
    int num;
    while (std::cin >> num)
    {
    	// ...
    }
    when i use this method it doesnt recognize the first number i input, it does recognize all the subsequent ones. should i put the cin>>input inside the brackets of the loop?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It might be because you have "cin >>input" twice in your program above. The only place that should be is inside the while control: while (cin >> input).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM

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