Jump to content

Validating user input in C++?

infernowheels

Use a one dimensional array to solve the following problem. Read in twenty numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it is not a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve for this problem.

 

 

I found someone else who had a similiar code and I've modified it, It works almost perfectly except for the fact that it still displays the numbers which had duplicates at the end.

#include <iostream>using namespace std;main(){      const int MAX = 20;        int a[MAX] = {0};      int j , k = 0;      int duplicate;      int num;      cout << "Enter 20 numbers between 10 and 100: " << endl;      for (int x = 0; x <= MAX - 1; x++ )	  {         duplicate = 0;         cin >> num;		 if (num > 10 && num < 100)		 {            for (j = 0; j < k; j++)		 {               if (num == a[j])               {                  duplicate = 1;				  cout << "Duplicate number." << endl;                  break;               }          }            if (!duplicate)		    {		    	 a[k++] = num;                    }          }	    else	    cout << "Invalid number." << endl;          }      cout << endl << "The non-duplicate values are: " << endl;;      for (int i = 0; a[i] != 0 && i < MAX; i++ )	  {         cout << a[i] << ", ";      }      cout << endl;system("pause");return 0;}

Also, since this wasn't really my code, could someone please explain what this part of the code is used for?

for (j = 0; j < k; j++)		 {               if (num == a[j])               {                  duplicate = 1;	          cout << "Duplicate number." << endl;                  break;               }

Thanks!

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

I don't have time to go trough whole code but it sounds like your problem is that program doesn't track how many unique members have left.

You could solve that by creating new int n = 20 at start and in part where duplicate = 1 you should decrement it by 1. Then last for will go to n and not to max.

Link to comment
Share on other sites

Link to post
Share on other sites

...

 

don't copy bad written code from someone else, if you don't even understand it.

 

 

Also, since this wasn't really my code, could someone please explain what this part of the code is used for?
for (j = 0; j < k; j++)		 {               if (num == a[j])               {                  duplicate = 1;	          cout << "Duplicate number." << endl;                  break;               }

Thanks!

 

used to check if the number is already in the array.

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know the syntax of c++ exactly but this is a basic problem used in many programming courses. I assume you just started to learn about arrays and I suggest you try to figure it out on your own instead of using someones code. What you have to do is the following:

 

You have an array of user input (x) and an array of validated data (y). Iterate trough x and compare for the value if its present in y. If it isn't add it to y.

Link to comment
Share on other sites

Link to post
Share on other sites

The code you are using is doing something quite different to what you want it to do. I recommend you start from scratch and write your own code. That way you will understand what your code actually does.

Link to comment
Share on other sites

Link to post
Share on other sites

I think the code you got is quite different from what you want it to do xD

 

If you're still having problem, don't code right away. Try to make a flowchart of your logic in a piece of paper before coding, then if you think the flow is right and makes sense, start coding.

| CPU: Ryzen 5 3600 | MoBo: MSI B450 Tomahawk Max | RAM: T-Force Delta RGB (2x8) 16GB 3200MHz (Black) | GPU: Gigabyte GTX 1660 Ti OC | Case: NZXT H500 (Black) | HDD: WD Black 2TB + Seagate Barracuda 4TB | SSD: Crucial MX500 2TB | PSU: Seasonic GX-550 | Monitor: 3x Asus VC239H |

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think the code you got is quite different from what you want it to do xD

 

If you're still having problem, don't code right away. Try to make a flowchart of your logic in a piece of paper before coding, then if you think the flow is right and makes sense, start coding.

Or pseudocode ^_^

Link to comment
Share on other sites

Link to post
Share on other sites

I think the code you got is quite different from what you want it to do xD

 

If you're still having problem, don't code right away. Try to make a flowchart of your logic in a piece of paper before coding, then if you think the flow is right and makes sense, start coding.

 

 

Or pseudocode ^_^

 

Add in UML and timing diagrams, and submit all 3 with your code for bonus marks! Your teacher will be blown away! :P

Link to comment
Share on other sites

Link to post
Share on other sites

Add in UML and timing diagrams, and submit all 3 with your code for bonus marks! Your teacher will be blown away! :P

 

This hahahaha, UML for a basic calculator program lol xD

| CPU: Ryzen 5 3600 | MoBo: MSI B450 Tomahawk Max | RAM: T-Force Delta RGB (2x8) 16GB 3200MHz (Black) | GPU: Gigabyte GTX 1660 Ti OC | Case: NZXT H500 (Black) | HDD: WD Black 2TB + Seagate Barracuda 4TB | SSD: Crucial MX500 2TB | PSU: Seasonic GX-550 | Monitor: 3x Asus VC239H |

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Add in UML and timing diagrams, and submit all 3 with your code for bonus marks! Your teacher will be blown away! :P

 

 

This hahahaha, UML for a basic calculator program lol xD

I don't think that they are in to OOP right now, tho :)

Link to comment
Share on other sites

Link to post
Share on other sites

I don't think that they are in to OOP right now, tho :)

That's no excuse! It'll only impress the teacher even more! They may just go ahead and give him 100 in the course after this assignment!

 

PS: It was a joke :P

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×