I have said program:

#include <iostream>
#include <fstream.h>
#include <conio.h>

using std::cout;
using std::cin;
using std::ios;
using std::endl;

int main()
{
    char first[6];
    char middle[16];
    char last[16];
    int counter = 0;
    char temp;

    cout << "Please enter your first, middle, and last name:\n>";

    cin.get(first, 6);
    cin.ignore(80,'\n');
    cin.ignore(80, ' ');

    std::toupper(first[0]);

    cout << first << "333\n";
    return 0;

}

I originaly had it inputting in everything using a much more complicated method, but then found that this one would be easier. at the moment, it is currently reading in data from the user constantly. it should ony read in 6 (I'll be changing that later... this program is in the debugging stage right now :P ) characters at a time, and capitalizing the first letter of each word. I'm gting to the middle/last name later.

I know this program looks simple, and thats because it's a homework assignment. so don't write this prgram for me, tell e what I shuld do to write it.

What it needs to be able to do at this stage is to read in data from the user and capitalize the first letter of the word. that's it...

I talked with Narue briefly on IRC, and she said:

[20:11:25pm] <> Wait, you're reading from cin though. I bet the stream is in an error state.

that bit has me confused... I don't know what she means by that.. So narue, if you can help me there, that would be great. :)

Thanks,
Derek Elensar

P.S. Please excuse my speling mistakes... this keyboard sucks :P

Dani AI

Generated

The loop/“infinite read” is caused by the second ignore. After cin.get(...) the newline remains (or input may have leftover characters); your first cin.ignore(..., '\n') eats the end of that line, but the following cin.ignore(..., ' ') then waits until it sees a space (or 80 chars). That call will block the program waiting for more input, which is why typing an extra space+Enter lets it continue. was pointing at the input/ignore logic; 's thread suggestion to rethink the approach is also on target.

A simpler, robust pattern is: read one full line, split it into words, then capitalize each word. This avoids fiddly ignores and fixed-size buffers:

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>

int main() {
    std::string line, first, middle, last;
    if (!std::getline(std::cin, line)) return 0;
    std::istringstream iss(line);
    iss >> first >> middle >> last;

    auto cap = [](std::string &s){
        if (!s.empty())
            s[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(s[0])));
    };

    cap(first); cap(middle); cap(last);
    std::cout << first << " " << middle << " " << last << "\n";
}

Notes and quick fixes:

  • std::toupper returns an int; assign it back and cast via unsigned char to avoid undefined behavior for negative char values.
  • If you really need to discard the rest of a line, use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') (requires <limits>).
  • If you only want to skip leading whitespace, std::cin >> std::ws; is handy.
  • Avoid legacy headers like <fstream.h> and nonstandard <conio.h>; prefer standard <iostream>, <string>, <cctype>, <sstream>.

These changes remove the need for the second blocking ignore and make the capitalization step reliable.

Recommended Answers

All 3 Replies

Type a string, then hit enter, then type a space and hit enter again. That'll get you through the maze of ignores. ;) What exactly are you trying to accomplish here?

First, I don't think Narue is a dude. Second, I had this same post about 2 days ago, you may want to check out that thread. here

Ok, that makes sense... how can I incorporate both the cin.ignore statements into one?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.