[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/g/ - Technology

Only inane comments

Catalog

8chan Bitcoin address: 1NpQaXqmCBji6gfX8UgaQEmEstvVY7U32C
The next generation of Infinity is here (discussion) (contribute)

You may buy ads now for any board, betakey is removed. Please contact ads@8ch.net for information or help with this service.
Email
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Flag
Embed
(replaces files and can be used instead)
Options
dicesidesmodifier
Password (For file and post deletion.)

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


RulesResources

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.

 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;

}


 No.2707

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


 No.2710


 No.2728

>>2706

your ifs are wrong.

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


 No.2729

>>2710

>>2728

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


 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;
}


 No.3277

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

>using namespace std;


 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


 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();}


 No.3322

>>3321

better yet

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




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]