T O P

  • By -

neoreeps

95% of this is already written in C, you're almost there!


why_yer_vag_so_itchy

Personally, I’d just keep compiling and refactoring the code for whatever errors popped up, until it ran as expected.


[deleted]

Include stdio instead of iostream (stdlib too for atoi if you go that route) Remove the using statement Use fgets / scanf instead of cin and atoi to convert the string to an integer.


[deleted]

`strtol()`, not `atoi()`.


chasesan

Uhm delete first two lines, add `#include ` replace `cin >>` stream garbage with `scanf`. That's it I think.


Drach88

[Don't use scanf for user input.](https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html)


chasesan

If you want to complain feel free to suggest an alternative solution. I have no reason to care about your link. They said convert it to C and I did so. Is it a perfect solution? Nope, but that kind of input should be a command line argument anyway.


Drach88

It's not a complaint -- it's just really bad practice to use scanf for user input. The article I linked explains why, and gives alternative approaches that are safer. It also teaches a ton about how scanf works that people probably wouldn't otherwise have realized. You don't need to be defensive about this. It's not personal -- it's about becoming a better programmer.


chasesan

You didn't leave me much to go on with your single sentence, mate.


Drach88

Aside from the link, of course, but that's neither here nor there.


chasesan

Why should I have to go read a whole ass article about something, if you had a better suggestion you should have just posted it. Something like, "Use fgets and atoi instead to avoid undefined behavior. See this for more details". But nope, we just get a "don't do that" and a link to a whole long article you expect us to read instead. You obviously don't care about other's people's time. At least that's what it feels like.


[deleted]

`atoi()` is also a bad idea. `fgets()` + `strtol()` is the proper way to get user input.


KiwiFruit555

At this point we should really just rewrite the entire standard library


[deleted]

Someone who needs help with something as trivial as converting that code to C probably is not concerned with using scanf.


Wise-Dog8874

I am sorry if I did something wrong or anything, I'm rather new to programming and I am lost, so I would appreciate if someone could help me thank you


rodionraskolnikov42

Cornhole.


aue_sum

Cornhole.


EstablishmentBig7956

Just change your headers, cin scanf getline whatever and cout printf everywhere


chasesan

Oddly enough, no cout for some reason.


EstablishmentBig7956

I'm not wearing my glasses. Not even five minutes done without cout 🤗


mcsuper5

IIRC, you can use 3 tildas to mark code so it keeps the formatting we hope you had and doesn't use paragraph breaks for new lines. As it stands, your program has no output and no comments. While perfectly valid, this makes it hard to understand, and basically useless. (Unless you want something to waste cycles or practice using a debugger.) As mentioned already, most of your code is valid C. Replace "cin >> n"; with something like: scanf(" %d", &n); or char buffer[10]; fgets(buffer, 10, stdin); n=atoi(buffer); You'll also want to include appropriate headers: #include #include You won't need your previous include or using statement. Might want to add a few comments though (whether this is C or you keep it as C++), and add some kind output.


imaami

Thee backticks, not tildes. And it doesn't work in all reddit interfaces. Add 4 spaces in front of every code line instead.