New Driver's Licence challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

New Driver's Licence challenge

I can't figure out what's wrong with my code so if anyone does please tell me. Heres my code in c++: #include <iostream> #include <string> #include <cmath> using namespace std; int main() { string name,people; int agents, time, add, help, result; char me; time=20; cin>>name; cin>>agents; cin>>people; me=name[0]; add=0; long unsigned int i=0; for (i=0 ;i<people.size(); i++) { if (isupper(people[i])) { if(people[i]<me) add++; } } help=round(add/agents); result=help*time; cout<<result; return 0; }

24th Jan 2020, 10:32 PM
Beks
Beks - avatar
11 Answers
+ 9
1. cin only reads up to the first whitespace. Considering that the last input is a string containing multiple whitespace characters, you might want to use std::getline() instead: https://en.cppreference.com/w/cpp/string/basic_string/getline If you do so, you will have to extract the remaining whitespace character(s) from the last input before using it, here the simplest way is to add std::cin >> std::ws; before the call. 2. You don't take into account that your own test also takes another 20 minutes. 3. The line add / agents is pure integer division and therefore will result in an integer. If you want a floating point value instead, at least one of the operands needs to be itself a floating point value, e.g. through casting: ( double )add / agents or static_cast< double >( add ) / agents 4. No portion of an agent can perform a test, you need a "full" agent for this. Consider this when rounding the result of your division.
25th Jan 2020, 12:07 AM
Shadow
Shadow - avatar
+ 1
Is there an error? Or are you not getting the expected results?
25th Jan 2020, 12:00 AM
Gevork Bagratyan
+ 1
if (add==0) cout<<20; else if (add%agents==0) cout<<add/agents*time; else cout<<add/agents*time+20; I did this and it worked!! Shadow thank you so much for your help!
25th Jan 2020, 4:00 PM
Beks
Beks - avatar
+ 1
Use cin.getline(string name,size); To get a string with whitespace
4th May 2020, 9:06 PM
View My Project
View My Project - avatar
0
There are no errors just wrong results
25th Jan 2020, 10:01 AM
Beks
Beks - avatar
0
Shadow I took your advice and changed it but result still remained the same. The problem is that the variable "add" always ends up a 0. long unsigned int i=0; for (i=0 ;i<people.size(); i++) { if (isupper(people[i])) { if(people[i]<me) add++; } } The problem must be in this part.
25th Jan 2020, 10:39 AM
Beks
Beks - avatar
0
Not necessarily. Could you perhaps link the entire latest version of the code? I tested the code with all the changes I mentioned and it passed the challenge.
25th Jan 2020, 12:24 PM
Shadow
Shadow - avatar
0
#include <iostream> #include <string> #include <cmath> #include <sstream> using namespace std; int main() { string name,people; int agents, time, add, result; double help; char me; time=20; cin>>name; cin>>agents; getline(cin, people); me=name[0]; add=0; std::cin>>std::ws; long unsigned int i=0; for (i=0 ;i<people.size(); i++) { if (isupper(people[i])) { if (people[i]<me) {add++;} } } help=(double)add/agents; result=help*time+20; cout<<result; return 0; } //here it is //if I have too many mistakes would you mind sending your code so that i could see what im doing wrong because i dont understand the std:: stuff much
25th Jan 2020, 12:33 PM
Beks
Beks - avatar
0
std:: is the standard namespace which encapsulates everything in the STL. Since you have using namespace std; included, you can omit it before all calls to stuff provided by the STL like std::cout and std::cin. The loop is not the problem, you need to put cin >> ws; right before the call to getline(). The point of this line is to extract remaining whitespace characters from the input buffer. If you don't include it, getline() will only read in the newline character remaining from your last input and stop afterwards, resulting in an empty string. Also, your logic fails in case the number of agents is equal to either one or your own position, because then you add 20 where it is inappropriate.
25th Jan 2020, 3:28 PM
Shadow
Shadow - avatar
0
перевод std:: - это стандартное пространство имен, которое инкапсулирует все в STL. С тех пор как вы это сделали использование пространства имен std; включенный, вы можете опустить его перед всеми вызовами к вещам, предоставляемым STL, таким как std::cout и std::cin. Петля - это не проблема, ее нужно поставить cin > > ws; прямо перед вызовом getline (). Смысл этой строки заключается в извлечении оставшихся пробелов из входного буфера. Если вы не включите его, getline () будет читать только символ новой строки, оставшийся от вашего последнего ввода, и остановится после этого, в результате чего получится пустая строка. Кроме того, ваша логика терпит неудачу, если число агентов равно либо одному, либо вашей собственной позиции, потому что тогда вы добавляете 20, где это неуместно.
9th Sep 2020, 6:16 PM
Rustam Yumagujin
Rustam Yumagujin - avatar
0
simple for loop, just you increment the index variable not by 1, but by the number of agents.
4th Dec 2020, 6:44 PM
Igor Bezverhi
Igor Bezverhi - avatar