Ending while loop at certain input and char comparison

So Im trying to solve the problem here: https://open.kattis.com/problems/acm
Im supposed to give teams a time score based on their results. The input is supposed to be in the form:
"minutes" "uppercase letter" "right/wrong", for instance: 30 B right, written on n number of lines. When there are no more lines, the user will input -1. I tried to use cin.get to end the while loop when this was input, but it did not work.

Also, i made a vector containing uppercase letters in the order they were input, and i wanna do a strcmp with elements in this vector and the uppercase letter that was most recently input, but i get the following error: No matching function for call to 'strcmp'. Candidate function not viable: no known conversion from 'char' to 'const char *' The strcmp where i compare str to "right" however works fine.

Here is my code:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 #include "ACM.hpp"
#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>

using namespace std;
int main() {
    
    
    int points = 0;
    int time=0;
    int t;
    char c;
   char str[5];
    vector<char> v;
    cin >> t >> c >> str;
    v.push_back(c);

    while (cin.get() != -1) {
        
   
        cin >> t >> c >> str;
        v.push_back(c);

        if (strcmp("right", str) == 0) {
            
            time += t;
            points += 1;
            
            for (int i=0;i<v.size();i++) {
                if (strcmp(c,v[i]) == 0) {
                    time +=20;
            }
            }
            
        }
                
            
    }

    cout << points  <<   time;
   
    cout << "\n";
    return 0;

}
Last edited on
1
2
3
4
5
6
7
8
9
#include <string>
#include <string.h>
#include <stdio.h>

std::string str;

if (str == "right") {

}
Last edited on
Im sorry i didn't specify. The strcmp where i compare str to "right" works just fine, its the second strcmp where i compare c and v[i] which doesen't work.
if ( c == v[i] )
Thanks. How do i make sure the while loop stops when user inputs -1 though?
Last edited on
Hi,
The strcmp where i compare str to "right" works just fine


I was trying to get you to use a string, not an array of char, and to use C++ facilities not C functions. In other words think in C++, not C - which is a different language.

.... and i wanna do a strcmp with elements in this vector and the uppercase letter that was most recently input ....


Does the time += 20; work properly? Where should the for loop be in the code? Under what circumstances is 20 added to the score?

How do i make sure the while loop stops when user inputs -1 though?


When you read the documentation for std::cin.get , what does it tell you for the way you are calling it? What other much more normal methods do you have for reading a number from the input?
I also tried using while (t !=-1) but that didn't work either. Im not sure what you're referring to with "much more normal methods for reading a number from the input". I tried using scanf instead for what its worth, but the result remained the same.

Every time they get a problem right i wanna check how many times they got the problem wrong previously, so i can add a 20 minute time penalty for each attempt they used at the problem they got right, hence the for loop.
Im not sure what you're referring to with


std::cin

Every time they get a problem right i wanna check how many times they got the problem wrong previously, so i can add a 20 minute time penalty for each attempt they used at the problem they got right, hence the for loop.


But you are doing that every time the while loop goes around, so the time added with the 20 will be huge.
While c.in != -1 ? I've already tried checking if the variable c that I take in equals -1, but that didn't work. I don't know how to use cin any other way to check whether -1 was input by the user.

My intention is that the for loop should only activate if the if(strcmp("right", str) == 0) condition is met. Im only checking for that specific letter they just got right, and i will only check for that letter once.
Last edited on
Topic archived. No new replies allowed.