Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm just beginning to learn c++ and i just need some Examples to solve this one to understand the way to program in c++
i have tried with an array but couldn't finish the program and i thing im totaly wrong
Pleas help me if you can!!!


Read in as many values as you like (reading in further values should be terminated by entering the value 0. Then specify how many of the entered numbers were currently, how many odd numbers and how many numbers were read in in total

What I have tried:

#include <iostream>
using namespace std;

int main(){
int n{0};
int size1{0};

cout<< "how many number you would like to enter"<<endl;
cin>> input;

if(input <1 || input >100){
cout<< " pleas enter a Number between 1 and 100"<<endl;
}

int *numbers = new int[input];
for (int i{0};i<input;i++){
cout << "Please enter entry " << i+1 << "." << endl;
cin>>numbers[i];


}




return 0;
Posted
Updated 6-Oct-18 2:50am

To fix your program:
C++
using namespace std;
int main()
{
  size_t size;

  do
  {
    cout  << "how many number you would like to enter"  <<  endl;
    cin >> size;

    if(size <1 || size >100)
    {
      cout  << " pleas enter a Number between 1 and 100"  <<  endl;
    }
  } while ( size < 1 || size > 100);


  int * number = new int[size];
  for (size_t i{0};  i < size ; ++i)
  {
    cout << "Please enter entry " << (i+1) << "." << endl;
    cin>>number[i];
  }

  // here you should provide requested output
  //...

}


However, for such a task, an array is not needed, simply keep track of how many even integers and how many odd integers the user enters.
 
Share this answer
 
Comments
Anas Zahed 8-Oct-18 3:29am    
Thank you !!!
i have solved it !!! thanks to your help
CPallini 8-Oct-18 3:50am    
You are welcome.
Quote:
i have tried with an array but couldn't finish the program and i thing im totaly wrong
Pleas help me if you can!!!

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Anas Zahed 8-Oct-18 3:27am    
Thanxxxxxxxxx alot for the Advive !!!
very cool exerciseing tool
Patrice T 8-Oct-18 4:47am    
Nice you like it.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! You don't need an array for this (because you don't know how many numbers you will read, you don't know how big to make the array!) Do it in easy stages:
Read in the data, and print it back out so you can see you read it right into a number. Test it. You will need a loop for this. When it works, take out the print code.
Then add counters: odd numbers, even numbers will do it. And add code to check if it's odd or even and increment the appropriate counter.
Then print the results you need.
It's not difficult, just break it into smaller bits, and each bit gets easier to do!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Anas Zahed 6-Oct-18 6:37am    
thanks a lot. it's just that i don't know how to begin with learning.
the Homework was very easy and i fund this example from an old homework and i thought i can exercise.
if you have some tipps where i can find material to exercise i will be very thanksfull
OriginalGriff 6-Oct-18 6:45am    
Get a book, and do the exercises in that as you come to them - that way the exercises are "leveled" to what you have been taught so far, and don't expect you to use concrete classes derived from abstract inside vectors when all you know about are arrays! :laugh:
Just "grabbing exercises" at random is a recipe for confusion - there is a lot to learn and you cant; do it at random!
Anas Zahed 6-Oct-18 6:48am    
that way i got confused the last day!!! thanks :)
OriginalGriff 6-Oct-18 6:54am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900