What Does Cin Read if Blank Line C++

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.
  • Forums
  • Other Sciences
  • Programming and Estimator Science

Why am I getting an actress blank line at the end of my output?

  • C/++/#
  • Thread starter YoungPhysicist
  • Outset date
Note: this is not a homework question!!
The problem that this plan should solve is turn a input like this:(no space and the finish of the input string)
                          james shawn charles nicholas                        
into something similar this:
                          James Shawn Charles Nicholas                        
Only I keep getting an excess line at the bottom of all outputs like this:
                          James Shawn Charles Nicholas                   <-This line                        
And so my code send an excess '\due north',but I tin can't find which office of my lawmaking went wrong.
My code:
                          #include<iostream> #include<string.h> using namespace std; char n[grand]; int lcv = 0; int main(){    cin.getline(n,300);    cout<<char(toupper(n[0]));    for(unsigned short lcv = 1;lcv<strlen(n)-one;lcv++){        while(north[lcv] != ' '){            if(lcv>=strlen(north))            break;            cout<<n[lcv];            lcv++;        }        cout<<'\northward'<<char(toupper(n[lcv+1]));        lcv++;    } return 0; }                        

Answers and Replies

The "break" statement but takes you out of the "while { ...} " code. It doesn't cause the program to skip the
                          cout<<'\n'<<char(toupper(north[lcv+ane]));                        
statement.
To elaborate on what @Stephen Tashi said, break exits the innermost enclosing loop, which in this example is the while loop. It doesn't get you out of the outer loop.

Some other comments:

  1. Why have you declared lcv twice? You have a global variable of type int with this name, and also a variable of the aforementioned proper noun of a dissimilar type that is local to the for loop. Having global variables like this is generally a bad practice.
  2. Your variable names are not well chosen -- n is non a adept proper name for a string variable. Unmarried-letter names for variables are unremarkably not good practice, except for loop control variables, which typically are called i, j, and the like. All other variable names should exist chosen then that a reader tin hands tell what they're supposed to stand for.
If you are reading your data from a text file then the new line \n is probable at the cease of your line.

Try nulling out the final character of your line of data and see if information technology goes away or subtract 1 from the string length.

Also be aware that on windows systems text lines will have a CRLF ie \r\north termination.

A problem I see with your lawmaking is you are just checking for space characters, then you will print the \n character at the end of the line as though it were office of the last word which it isn't. For the cleanest lawmaking, you need a better bank check there. I personally would check for " \t\r\n" which should be adept enough.
That is horrible. Try
                          #include<iostream> #include<cord.h> using namespace std; char input[g]; int main(){  cin.getline(n,300);  bool newword = true; /* If bool is not available (C < C99) use an int */  for (int i = 0; i < strlen(n) - i; i++) {   if (n[i] == ' ') {    newword = 1;   } else if (newword) {    newword = false;    cout<<'\n'<<char(toupper(n[i]));   } else {    cout<<n[i];   }  } render 0; }                        
Sorry, didn't have a C compiler handy to test.
                          #include <iostream> #include <cord.h>  int principal(){   // Get the input string.   char input[m];   std::cin.getline(input, 1000);    // Ready a flag for the get-go discussion (if bool is not available (C < C99) use an int).   bool newword = true;   for (int i = 0; i < strlen(input); i++) {     // A infinite marks a new word only do not print it.     if (input[i] == ' ') {       newword = true;     // Print the current character, capitalizing if necessary.     } else if (newword) {       // Reset the flag for the next graphic symbol.       newword = false;       std::cout << '\n' << char(toupper(input[i]));     } else {       std::cout << input[i];     }   }   render 0; }                        
C++ has had the std::cord data type for 20 years (probably since before YoungPhysicist was born :oldwink:). Which C++ textbook/resource is he/she using, that still uses C-strings (char arrays)?
                          #include <iostream> #include <string>  int main () {     std::cout << "Enter a listing of words on a single line:" << std::endl;     std::string line;     std::getline (std::cin, line);      // Does non effort to consolidate multiple spaces into a unmarried newline.      for (int k = 0; one thousand < line.size(); ++k)     {         if (line[k] == ' ')             std::cout << '\north';         else             std::cout << line[one thousand];     }     std::cout << std::endl;     return 0; }                        

Using the C++ 2011 standard, you lot can loop directly through the characters without using an index variable:
                          #include <iostream> #include <cord>  int main () {     std::cout << "Enter a list of words on a single line:" << std::endl;     std::cord line;     std::getline (std::cin, line);      // Does non attempt to consolidate multiple spaces into a single newline.     // Uses the range-based 'for' loop from C++ 2011 standard.      for (char thisChar : line)     {         if (thisChar == ' ')             std::cout << '\due north';         else             std::cout << thisChar;     }     std::cout << std::endl;     return 0; }                        

Oops, I forgot you want to capitalize the first alphabetic character of each word (name).
C++ has had the std::string information type for xx years (probably since earlier YoungPhysicist was born :oldwink:). Which C++ textbook/resource is he/she using, that still uses C-strings (char arrays)?
                              #include <iostream> #include <string>  int primary () {     std::cout << "Enter a list of words on a single line:" << std::endl;     std::string line;     std::getline (std::cin, line);      // Does non effort to consolidate multiple spaces into a single newline.      for (int k = 0; m < line.size(); ++chiliad)     {         if (line[k] == ' ')             std::cout << '\n';         else             std::cout << line[k];     }     std::cout << std::endl;     return 0; }                            

Using the C++ 2011 standard, yous can loop direct through the characters without using an index variable:
                              #include <iostream> #include <string>  int main () {     std::cout << "Enter a list of words on a single line:" << std::endl;     std::string line;     std::getline (std::cin, line);      // Does not effort to consolidate multiple spaces into a single newline.     // Uses the range-based 'for' loop from C++ 2011 standard.      for (char thisChar : line)     {         if (thisChar == ' ')             std::cout << '\due north';         else             std::cout << thisChar;     }     std::cout << std::endl;     return 0; }                            

Oops, I forgot you want to capitalize the first letter of each word (proper name).
Oops,I simply forgot it.:nb)(and all previous programs in my life)
If yous tin connect to this forum's servers via cyberspace,endeavour this 1...
Thanks for the suggestion, although I currently use Repl.information technology.

EDIT: Mentor added lawmaking from repl.information technology for posterity:

                            #include <iostream> #include <string.h>  int primary(){ // Get the input string. char input[1000]; std::cin.getline(input, 1000);  // Set up a flag for the starting time word (if bool is not available (C < C99) use an int). bool newword = true; for (int i = 0; i < strlen(input); i++) { // A space marks a new give-and-take simply do not print it. if (input[i] == ' ') { newword = truthful; // Print the electric current character, capitalizing if necessary. } else if (newword) { // Reset the flag for the adjacent character. newword = false; std::cout << '\northward' << char(toupper(input[i])); } else { std::cout << input[i]; } } return 0; }                          
Last edited past a moderator:
Thanks for the suggestion, although I currently utilise Repl.it.
Great 1!!
I stick to one-time-fashioned C. There is already a string role in the library chosen strtok:
                          char *strtok(char *restrict s1, const char *restrict delimiters);                        
A sequence of calls to strtok() breaks the cord pointed to by s1 into a sequence of tokens, each of which is delimited by a byte from the string pointed to by delimiters. The offset call in the sequence has s1 equally its first argument, and is followed by calls with a null pointer as their beginning argument. The separator string pointed to by delimiters may exist different from call to call.

Read more in https://en.wikibooks.org/wiki/C_Programming/String_manipulation

Related Threads on Why am I getting an actress blank line at the stop of my output?

  • Final Post
H Smith 94
  • Last Mail service
newjerseyrunner
  • Concluding Post
  • Last Postal service
  • Terminal Post
Mark44
  • Last Postal service
Silicon Waffle
  • Terminal Post
jbriggs444
  • Last Post
Mark44
  • Last Post
  • Terminal Postal service
  • Forums
  • Other Sciences
  • Programming and Information science

helmshentwonce94.blogspot.com

Source: https://www.physicsforums.com/threads/why-am-i-getting-an-extra-blank-line-at-the-end-of-my-output.956931/

0 Response to "What Does Cin Read if Blank Line C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel