0

Sorry if this is obvious but I'm very new to c++, thanks in advance

#include <iostream>

bool conduct_it_support(bool on_off_attempt) {
  std::cout << "Have you tried turning it off and on again? (true / false)\n";
  std::cin >> on_off_attempt;
  if(on_off_attempt == false) {
    return false;
  }
  else {
    return true;
  }
  return on_off_attempt;

}

int main() {

  bool attempt;
  conduct_it_support(attempt); {
    std::cout << "so it was " << attempt << "?\n";
  }

}

I excpect this to be either: "so it was true/false?" Sorry if this is obvious but I'm very new to c++, thanks in advance

Sakari
  • 21
  • 5
  • 1
    How exactly do you imagine this code works? `attempt` is never initialized and never assigned a value, it's value is undefined, the program has undefined behavior… – Michael Kenzel Mar 24 '19 at 19:28
  • Try this: `bool attemp = conduct_it_support(false);` – Amadeus Mar 24 '19 at 19:43
  • Apart from what @Amadeus said, I would highly recommend that you try to step through this code with a debugger and compare your expectations with what the debugger shows you is actually happening. I think this would greatly aid your understanding, more than just being presented with the correct code ever could… – Michael Kenzel Mar 24 '19 at 19:46

2 Answers2

1

By default the stream class(s) will serialize bool as 0 or 1. They will also read 0 or 1 when de-serializing.

To make it print the string(s) true or false you need to use a stream modifier std::boolalpha to change the behavior of the stream to print (or read) the text version of boolean values.

See below:

#include <iostream>
#include <iomanip>

int main ()
{
  bool a = false;
  bool b = true;
  std::cout << std::boolalpha   << a << " : " << b << '\n';
  std::cout << std::noboolalpha << a << " : " << b << '\n';


  // If you want to read a bool as 0 or 1
  bool check;
  if (std::cin >> std::noboolalpha >> check) {
      std::cout << "Read Worked: Got: " << check << "\n";
  }
  else
  {
      std::cout << "Read Failed\n";
  }

  // PS. If the above read failed.
  //     The next read will also fail as the stream is in a bad
  //     state. So make the above test work before using this code.


  // If you want to read a bool as true or false
  bool check;
  if (std::cin >> std::boolalpha >> check) {
      std::cout << "Read Worked: Got: " << check << "\n";
  }
  else
  {
      std::cout << "Read Failed\n";
  }

}
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Thanks, but my problem is mostly about the fact that I get only false as output and can you reopen this because I changed the title? – Sakari Mar 24 '19 at 19:59
  • @Sakari: That's because you don't assign a value to `attempt` before printing it. The output of the function `conduct_it_support()` is not assigned to any variable. – Martin York Mar 24 '19 at 20:03
  • Try: `bool attempt = true; attempt = conduct_it_support(attempt);` – Martin York Mar 24 '19 at 20:04
  • it will just return in true all the time – Sakari Mar 24 '19 at 20:33
  • @Sakari: The point is that at the moment you are not assigning anything to it (attempt). What do you expect it to print? To make it print something else assign a different value to it. – Martin York Mar 24 '19 at 20:59
  • On the intput: `std::cin >> on_off_attempt` it is expecting you to type `0` or `1`. If you want to type `false` or `true` then you need to use the `boolalpha` manipulator. `std::cin >> std::boolalpha >> on_off_attempt;` – Martin York Mar 24 '19 at 21:03
  • PS. You should also check the read worked. – Martin York Mar 24 '19 at 21:03
  • Add updated code. – Martin York Mar 24 '19 at 21:07
  • now I get it! I will try printing the true/false value somehow else, and try the boolalpha. Thank you – Sakari Mar 25 '19 at 06:28
  • Thank you so much! I got it working, and I would otherwise show it here, but I don't know the syntax to posting code here. Do I need to remove/close this post somehow now? – Sakari Mar 25 '19 at 12:02
-4

This code is working fine:

bool conduct_it_support(bool init) {
  bool on_off_attempt=init;
  char selectVal[10] = "000000000";
  std::cout << "Have you tried turning it off and on again? (true / false)\n";
  std::cin >> selectVal;
  if(selectVal == "false") {
    on_off_attempt=false;
  }
  else {
    on_off_attempt=true;
  }
  return on_off_attempt;
}

int main()
{
    bool attempt;
    attempt = conduct_it_support(attempt); {
    std::cout << "so it was " << attempt << "?\n";
    }

Try this code here.

  • `Try this code` only answers without explaining what you have changed and why are not really helpfull. – t.niese Mar 24 '19 at 19:38
  • 2
    I doubt you even tried this code yourself as it won't compile due to a typo. Even if it would compile, `bool attempt` is not initialized and not assigned a value here either. It will still have the exact same issue as the original code, which was that the value of `bool attempt` is undefined… – Michael Kenzel Mar 24 '19 at 19:39
  • Thanks for helping me, but this gives me error about selectVal being undeclared, even though it's clearly declared? and what do those init's mean? – Sakari Mar 24 '19 at 19:46
  • @Sakari make correction: char[10] selectVal = "0000000000"; let it be equal to any string, with 10 letters. – Saumitra Topinkatti Mar 24 '19 at 19:47
  • ` char[10] selectVal = "0000000000";` is a syntax error and even if you would write `char selectVal[10] = "0000000000";` it would still not correct, because you try to store 11 chars in an array with a capacity for 10 chars. – t.niese Mar 24 '19 at 19:55
  • 1
    as far as I know comparing a char* to a static string is not the way to compare the contents of a cstring with a constant. try using strcmp instead please. – FalcoGer Mar 24 '19 at 19:56
  • @SaumitraTopinkatti I still get the problem with selectVal being undefined and it excepts identifiers and semicolons at ´ char[10] selectVal = "0000000000"; ´ – Sakari Mar 24 '19 at 19:57
  • @Sakari code works fine now. – Saumitra Topinkatti Mar 24 '19 at 20:02
  • @SaumitraTopinkatti I still get an error "uninitialized local variable 'attempt' used" – Sakari Mar 24 '19 at 20:06
  • @Sakari Then replace "bool attempt;" with "bool attempt=false;" – Saumitra Topinkatti Mar 24 '19 at 20:08
  • but then it returns with a false all the time – Sakari Mar 24 '19 at 20:32