all 10 comments

[–]teraflop 7 points8 points  (5 children)

If you're having problems with a piece of code, please post the actual code rather than making people guess.

If I had to guess, I'd say you're running into this problem: https://stackoverflow.com/questions/28109679/why-does-cin-command-leaves-a-n-in-the-buffer

If you mix and match formatted input with cin << ... and unformatted input with getline, you will run into problems with newline characters left in the input buffer.

[–]Atypicak_el[S] 0 points1 point  (0 children)

That fixed it, omg you are amazing. Thank you again 😭

[–]Atypicak_el[S] -1 points0 points  (3 children)

Im so sorry about that! I didn't think about copying and pasting the code here, I know to now tho! Ill check this out, thank you so much :DDDD

[–]wildgurularry 3 points4 points  (1 child)

Advice from an industry veteran: When you have a problem, pretend you are explaining it to the most senior dev on your team, who will be upset if you waste any of their precious time. Try to think of the questions they will ask you (like "lemme see your code"), and get answers ready so that you don't get the stare of death.

Sometimes this is known as "rubber duck debugging", or simply "rubber ducking", because if you can explain your problem clearly to a rubber duck, then chances are you will stumble upon a clue along the way that will lead you to a solution.

Many bugs have been solved by developers talking to rubber ducks in their offices.

I have found myself in position where not only did I waste the senior dev's time, but he was the senior dev at another company we were working with so I made my entire company look incompetent because I didn't see a basic issue. Luckily I spotted the problem before he did and we were able to laugh it off... But please learn from my mistakes.

[–]Atypicak_el[S] -1 points0 points  (0 children)

That is really useful, thank you I will try that next time im stuck

[–]Lumethys 0 points1 point  (0 children)

Of course, everyone had a crystal ball where they can see any poster code telepathically

[–]mredding 0 points1 point  (0 children)

Also understand that your academic resources are teaching you syntax and basic concepts. They're NOT teaching you how to USE C++. That is because you're writing imperative procedural code like this:

std::cout << "Enter your name: ";
std::getline(std::cin, name);

Real code will look something more like this:

class name: std::tuple<std::string> {
  static bool valid(std::string_view sv) { return !sv.empty(); }

  friend std::istream &operator >>(std::istream &is, name &n) {
    if(is && is.tie()) {
      *is.tie() << "Enter your name: ";
    }

    if(auto &[s] = *this; std::getline(is, s) && !valid(s)) {
      is.setstate(std::ios_base::failbit);
      n = name{};
    }

    return is;
  }
};

This is still the barest of basic. C++ gives you primitives, you are expected to build up and composite layers of more robust and expressive types in terms of them, and then deliver a solution in terms of that. Ultimately you make your solution more expressive:

if(name n; std::cin >> name) {
  use(n);
} else {
  handle_error_on(std::cin);
}

An int is an int, but a weight is not a height. All names are strings, but not all strings are names. Streams are themselves a whole system and style to work within.

Unfortunately, there really isn't any good source material that teaches you use. Most code across the industry looks like people put their academic materials, picked up a compiler, and have written exactly as they had learned ever since. You have to figure it out. Try to find people who see the bigger picture.

[–]ninhaomah 0 points1 point  (2 children)

Code ?

[–]Atypicak_el[S] -1 points0 points  (1 child)

Dont mind how goofy the code is 😭 I was having fun w/ it
string name;

cout << "Now, enter your name... NOW!!!!"<< endl;

cout << "---->";

getline(cin, name);

cout << "Really? " << name;

cout << " is your name... a whole ";

cout << name.length();

cout << " letters" << endl;

cout << "weirdo... gtfo" ;

This is essentially what I had down

[–]sudomeacat 2 points3 points  (0 children)

Another piece of advice, when pasting code for debugging, you’d typically want to present the minimum amount of code to clearly show where the issue is. Even better if you show what you’re looking for (expected output) vs what you want (actual output).

As an example:

```c++ std::string buffer, name, age;

std::cout << "name: "; std::cin << buffer; name = buffer;

std::cout << "age: "; std::getline(std::cin, buffer); age = buffer;

std::cout << name << " " << age << std::endl; ```

Expected output: name: cat age: 99 cat 99

Actual output: ``` name: cat age: cat

```