[ / / / / / / / / / / / / / ] [ dir / ausneets / brit / britfeel / clang / cow / cyoa / india / sapphic ]

/g/ - Technology

Make /g/ Great Again
Name
Email
Subject
Comment *
File
Password (Randomized for file and post deletion; you may also set your own.)
Archive
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Voice recorder Show voice recorder

(the Stop button will be clickable 5 seconds after you press Record)
Options
dicesidesmodifier

Allowed file types:jpg, jpeg, gif, png, webm, mp4, swf, pdf
Max filesize is 16 MB.
Max image dimensions are 15000 x 15000.
You may upload 5 per post.


File: 1435848845661.jpg (131.3 KB, 960x720, 4:3, wot.jpg)

 No.2705

Hey /g/, I'm learning c++ from a book, and to make sure I knew what I've learned so far well enough to move on, I've tried writing a simple program that calculates the amount of time it would take to sing the "bottles of beer on the wall" song, in the users time measurement unit of choice.

However, I've run into a problem that I can't seem to find a solution for. Whenever I run the code, no matter what I put in for the time unit, it measures in seconds. I'll post it below.

My compiler isn't giving me any errors.

____________________________
Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.2706

#include <iostream>

#include <string>

int main()

{

long unsigned int numberOfBottles;

long unsigned int timeLength;

float singingTime = 8.5;

std::string timeUnit = "blank";

std::cout << "What would you like to measure time in?";

std::cout << std::endl;

std::cin >> timeUnit;

if(timeUnit == "Seconds" || "seconds" || "Seconds." || "seconds." || "SECONDS" || "SECONDS.")

{

std::cout << "How many bottles of beer are on the wall?";

std::cout << std::endl;

std::cin >> numberOfBottles;

std::cout << "It would take " << (numberOfBottles*singingTime) << " seconds to sing the song.";

}

else if(timeUnit == "Minutes" || "minutes" || "Minutes." || "minutes." || "MINUTES" || "MINUTES.")

{

std::cout << "How many bottles of beer are on the wall?";

std::cout << std::endl;

std::cin >> numberOfBottles;

std::cout << "It would take " << ((numberOfBottles*singingTime)/60) << " minutes to sing the song.";

}

else

{

std::cout << "Wat.";

}

std::cout << std::endl;

std::cout << "Type 'Exit' to exit.";

std::cout << std::endl;

char response;

std::cin >> response;

return 0;

}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.2707

Shit, I don't know why I have an int called "timeLength" in there.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.2710

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.2728

>>2706

your ifs are wrong.

instead of 'timeUnit == "Minutes" || "minutes...' do 'timeUnit == "Minutes" || timeUnit == "minutes"...'.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.2729

>>2710

>>2728

Oh man, I had no clue. Now I know. Thanks guys

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.3033

>>2729

Bear in mind that in C/C++ any non-0 value will evaluate to true...so your or'd string literals effectively become:


if(timeUnit == "Seconds" || true || true || true || true || true)

More importantly, comparing string variations like this is an exercise in insanity - there are too many combinations to effectively parse (what if a grill finds your program and enters "SeCoNdS"?).

Consider normalizing your input string to all lowercase and strip punctuation/trim whitespace:


#include <algorithm>
#include <string>
#include <cctype>

// this is the fancypants way of doing it

std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(), ::tolower);

// other processing is left as an exercise to the reader

if(data == "abc")
{
// do stuff
}

You could also match using regular expressions, though I haven't really used regexes in C++ (more in scripting Python or JS):


// you'll need C++11 for this
// if using gcc, add the "-std=c++11" switch
#include <iostream>
#include <regex>
#include <string>

int main()
{

std::cout << "oh herro" << std::endl;

std::string str = "pUmPkIn SpIcE lAtTeE SECONds omg ponies";

// Pattern to match against,
// with case insensitive flag
std::regex expr(".*(second|secs).*",
std::regex_constants::icase);

if(std::regex_match(str, expr))
std::cout << "Yes" << std::endl;

return 0;
}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.3277

if you simply used the namespace standard, there would be no need for the incessant std::

>using namespace std;

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.3310

>>2706

Heh took me 2 seconds

>if(timeUnit == "Seconds" || "seconds" || "Seconds." || "seconds." || "SECONDS" || "SECONDS.")

Pass in seconds

>std::cout << "It would take " << (numberOfBottles*singingTime) << " seconds to sing the song.";

output as seconds...check

> else if(timeUnit == "Minutes" || "minutes" || "Minutes." || "minutes." || "MINUTES" || "MINUTES.")

pass in minutes

>std::cout << "It would take " << ((numberOfBottles*singingTime)/60) << " minutes to sing the song.";

Output is minutes/60 which we on earth call seconds, but you call it minutes....fail

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.3321

>>2706

i cant remember the exact way to lowercase strings in C++ but this is a better way to make things case insensitive (and is used by most programmers)


if (timeUnit.toLower() == "seconds." || timeUnit.toLower() == "seconds"){doSomeCode();}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.3322

>>3321

better yet

//when in doubt, paran group
if ((timeUnit.toLower() == "seconds.") || (timeUnit.toLower() == "seconds")){doSomeCode();}

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.7212

File: e92ceeccf1d3996⋯.png (59.47 KB, 498x330, 83:55, Fri Jun 22 14:51:26 2018.png)

>>2706

To expand on >>2710, it doesn't look at the characters in the string. A string literal is just a char*, and this is what the if statement looks at. Because the pointer isn't nullptr, it evaluates to true. So it would still evaluate to true even with an empty string.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.7213

>>7212

Interestingly,


if (*"") cout << "yes" << endl;

...doesn't print anything because it '*""' points to the first character of the string, which is a null terminator (0.) String literals in C/C++ always end in a null terminator. If the string was anything but empty, you'd get true. You're better off using .empty() on C++ string variables though.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.7218

>>3321

if it's in a std::string then you can do

std::transform(timeUnit.begin(), timeUnit.end(), timeUnit.begin(), ::tolower);

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.7223

>>3310

minutes / 60 are hours

seconds / 60 are minutes

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.

 No.7293

>>2706

>if(timeUnit == "Seconds" || "seconds" || "Seconds." || "seconds." || "SECONDS" || "SECONDS.")

>else if(timeUnit == "Minutes" || "minutes" || "Minutes." || "minutes." || "MINUTES" || "MINUTES.")

just convert it to lowercase before testing it.

Disclaimer: this post and the subject matter and contents thereof - text, media, or otherwise - do not necessarily reflect the views of the 8kun administration.



[Return][Go to top][Catalog][Nerve Center][Post a Reply]
Delete Post [ ]
[]
[ / / / / / / / / / / / / / ] [ dir / ausneets / brit / britfeel / clang / cow / cyoa / india / sapphic ]