Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Eg.

User input: 2 2 3 3 3

Program displays: Correc!

--------------------------------------

User input: 2 2 4 4 4

Program displays: 4 has to show up 4 times

then the program needs to show the minimum amount of numbers it has to change in order to be correct. (Eg. 1 has to be present one time, 2 two times, 3 three times and so on)

Please help.

What I have tried:

#include <iostream>
#include <string>
using namespace std;

int main()
	{
	
		int n;
		string a;
		
		cin >> n;
		cin >> a;
		
	}
Posted
Updated 1-Jan-19 5:58am

Why are you reading an integer first, what is it supposed to tell you? When you read the string you just need to go through it character by character (use a subscript) and count the successive digits. If the count matches the actual digit (convert the first one to an integer for the comparison) then the count is correct. If you get an invalid count before reaching the end then reject the string.

But as it's your homework we are not going to write the actual code.
 
Share this answer
 
You need to look into what methods std::string has available. One of them is length(). It will tell you how long the string is. Another is the [] operator which will give you the character at a given location. With these things, and assuming you have a bunch of numbers in the string a, you can write logic like this:
C++
int alength = a.length();
for( int i = 0; i < alength; ++i )
{
    char cvalue = a[i];
    if( cvalue == ' ' )
        continue;      // skip spaces

    // the character is not a space
    // do something with it here
}
Exactly what you do with the characters there, I can't tell you because the description of your problem is lacking. For example, what does "correct" for your problem mean?
 
Share this answer
 
Comments
Member 14081190 31-Dec-18 13:43pm    
Correct means that for example if the user inputs 1 once or 2 twice or 3 thrice and so on. And the integer n is the amount of numbers the user will input, because the program has to change the minimum ammount of numbers so they will be "correct". It has to output how many times it needs to change numbers (Eg. User: 5 || 1 2 3 3 3 || Output: 1|| Because it has o change 1 to 2 in order to achieve the correct answer. Hope that helps you and can you please explain it to me with the code.
Store all input in a vector and than use a comparison to count the instances. At first make a search to find unique data and than count these.
 
Share this answer
 
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!

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
Member 14081190 31-Dec-18 12:42pm    
This is not homework. I am doing a project and I am stuck.
OriginalGriff 31-Dec-18 13:56pm    
What on earth makes you think "a project" isn't homework? Of course it is!
Member 14081190 31-Dec-18 13:59pm    
A home project, only for myself. I am trying to get better in C++ but I am stuck on this.
OriginalGriff 31-Dec-18 14:02pm    
You aren't "stuck" - you haven't started!
Member 14081190 31-Dec-18 14:05pm    
Because I don't know how to start and I'm seeking help.

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