1

I am very new to programming and I am trying to write a program in C++ that will convert text inputted by the user into HTML code and output that code onto a text file. I have already written the basic program and it works great. The only problem I am having is being able to input spaces in between words. For example, if you were to type into the program "HELLO WORLD", it would only display "HELLO" on the text file. I understand what the problem is, cin will read the text and stop at the whitespace, but I am trying to find out if there is any way around this? Any advice would be greatly appreciated, thank you.

The code I have written:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <string.h>

using namespace std;

int main()
{
    ifstream( inputFile );
    ofstream( outputFile );
    char title[500];
    char heading[500];
    char color[10];
    char body[5000];

    outputFile.open("html.txt");

    cout << "Please enter a title: ";
    cin >> title;
    cout << "Please enter a heading: ";
    cin >> heading;
    cout << "What color would you like your heading to be? ";
    cin >> color;
    cout << "Please enter the body: ";
    cin >> body;

    outputFile << "<!doctype html> " << "\n" << "<html> " << "\n" << "<head> " << "<title> " << title << " " << "</title> " << "</head>" << "\n"
           << "<body> " << "\n" << "<h1> " << "<center> " << "<font color=\"" << color << "\"> " << heading << " " << "</font> " << "</center> " << "</h1> " << "\n"
           << body << " " << "\n" << "</body> " << "\n" << "</html> " << "\n";


    outputFile.close();

    system("PAUSE");
    return 0;

}
BCRwar1
  • 189
  • 5
  • 21

3 Answers3

3

Here in your code you want to read the whole line entered, as far I understood your question well

cout << "Please enter the body: ";
std::getline(cin,body);

Instead of declaring body as a plain char array (char body[5000];), you have to use a std::string instead:

std::string body;

Thus you get rid of bothering with estimating and reserving the maximum possible input length you could get.

NOTE:
The above sample just enables you to input a single line of HTML text, if you wan't to have multiline input, you either need to chose a different line delimiter (than the default '\n'), or accumulate subsequent inputs to body until e.g. an empty line or some other appropriate end marker is entered:

std::string body;
cout << "Please enter the body (stop entering a blank line): ";

std string line;
std::cin.clear(); // Get rid of all previous input state
do {
    std::getline(cin,line);
    if(line.empty()) {
        break; // Stop the input loop
    } 
    else {
        body += line + "\n"; // accumulate the entered input to 'body'
    }
} while(1);
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • @BenjaminLindley Yeah just noticed this, and edited my answer accordingly! – πάντα ῥεῖ Jun 22 '14 at 22:39
  • I am still having trouble, I tried "std::getline(cin,body)" and I also tried changing body to a string, I keep getting errors that say something like "no matching function for call to 'getline(std::istream&,std::string[5000]) – BCRwar1 Jun 22 '14 at 22:52
  • 1
    @BCRwar1 _'I am still having trouble ...'_ I didn't asked you to replace `char body[5000];` with `std::string body[5000];` but just `std::string body;`!!! – πάντα ῥεῖ Jun 22 '14 at 22:55
  • 1
    Also note that after this change, the OP is likely to run into this common issue: http://stackoverflow.com/questions/6642865/getline-not-asking-for-input/ -- It would probably be best to use getline for all the inputs (which will incidentally solve that issue). After all, why not allow the user to input multi-word titles and headings? – Benjamin Lindley Jun 22 '14 at 23:07
  • Okay so I have been playing around with it and I just cant get it to work, I changed char body[5000] to string body, I tried std::getline(cin,body) instead of just cin >> body and it compiles, but when it asks me to enter the body, it won't let me input anything, it just ends the program – BCRwar1 Jun 22 '14 at 23:46
  • @BCRwar1 Could easily be, you've been hit of what BenjaminLindley anticipated in his comment: http://stackoverflow.com/questions/6642865/getline-not-asking-for-input/ Please don't expect me to give you a fully working solution on such poorly asked question and (in various ways) flawed code examples. I still think, I gave the main necessary pointers, what to do about your `body`. – πάντα ῥεῖ Jun 22 '14 at 23:54
  • @BCRwar1 Also note I made a small update `std::cin.clear();` that could help solving this problem ... – πάντα ῥεῖ Jun 23 '14 at 00:04
  • 1
    Okay so I did check out that link that Benjamin Lindley suggested and it worked, and I now see why you suggested std::cin.clear(), thank you for your help! – BCRwar1 Jun 23 '14 at 00:19
  • I've upvoted your question in response, that you were really showing engagement and putting efforts in finding the solution. Your question still might need a bit more improvement (the title in particular), to be really helpful for others researching on the topic. But that's minor (please don't go now and change your question essentially, especially not the code that was originally posted). – πάντα ῥεῖ Jun 23 '14 at 00:25
1

Try using cin.getline(body,5000)

Anil
  • 21,730
  • 9
  • 73
  • 100
101
  • 62
  • 1
  • 6
0

you should maybe have a look to cin.getline function .. see this example (found on stackoverflow)

#include <iostream>
using namespace std;

int main () {

  char name[256], title[256];

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

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
}
Community
  • 1
  • 1