473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can't get my program to ignore blank lines.

I'm trying to write a program that creates webpages by reading text from a file. I can't seem to get the program to ignore lines that only contain the "\n" or "\t".

Here is the main section of code.

int main()
{
string sarray[6];
int counter=0;
ifstream infile;
ofstream outfile;
//string filename; filename.c_str()
string temp;
infile.open("from_excel.txt", ios::in);
outfile.open("output.htm",ios::out);

while(getline(infile,temp))
{
counter=0;
cout<<"Main Counter = "<<counter<<"!!!\n\n";
while((temp!="\n") || (temp!=" ") ||(temp!="\t"))
{
cout<<"counter = "<<counter<<endl;
cout<<"temp = "<<temp<<endl;
sarray[counter]=temp;
counter++;
getline(infile,temp);
}

cout<<"22222 Main Counter = "<<counter<<"\n\n";
create_page(counter,sarray,outfile);
}
infile.close();
outfile.close();

return 0;
}

The way this should work is if temp is a newline, space or tab it shouldn't go into the while loop but it has been regardless of what is on the line.
Any help is greatly appreciated.

PS. The cout statements were for error testing purposes.
Dec 27 '07 #1
5 2464
scruggsy
147 100+
while((temp!="\n") || (temp!=" ") ||(temp!="\t"))
Look at this condition. Consider the case where temp == '\n':
Expand|Select|Wrap|Line Numbers
  1. while ("\n" != "\n" || "\n" != " " || "\n" != "\t")
|| means OR. "\n" != "\n" returns false, but "\n" != " " returns true, thus the expression evaluates to true, thus execution enters the loop.
Use && instead of ||.
Dec 27 '07 #2
xoinki
110 100+
hi,
I can think of 2 reasons:
1) Either you need to check for character '\n' instead of String "\n"
2) In widows carriage return is '\r\n' and In linux it is '\n' and in MAC it is '\r' If I correctly remember. My suggestion would be to put a condition that include '\r\n'.

Thnks and Regards,
Xoinki
Dec 28 '07 #3
Look at this condition. Consider the case where temp == '\n':
Expand|Select|Wrap|Line Numbers
  1. while ("\n" != "\n" || "\n" != " " || "\n" != "\t")
|| means OR. "\n" != "\n" returns false, but "\n" != " " returns true, thus the expression evaluates to true, thus execution enters the loop.
Use && instead of ||.
I tried replacing with && but that didn't work. In addition to that problem, My program is randomly not reading lines from the file.
Do I need to somehow clear any of the i/o streams?
Dec 28 '07 #4
I tried replacing with && but that didn't work. In addition to that problem, My program is randomly not reading lines from the file.
Do I need to somehow clear any of the i/o streams?

Ok so I fixed the not reading in lines issue, but it still doesn't recognize the lines that only contain newlines or tabs. Am I comparing them correctly?
Dec 28 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
I believe you need to find the string size. If it's 1 and string[0] is equal to '\n' or '\t', then you should have your guy.
Dec 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
0
by: Suzi Carr | last post by:
Hello, We have a basic report that prints a customer's name followed by their order detail lines (part#, qty, price, ext price, etc). We setup the detail section to print the rows tightly...
60
by: Dominique Léger | last post by:
Hello guys, I'm kinda new to C, and I'm having a hard time with strings. What I'm trying to do is a simple function that trims spaces & tabs at the beginning of a given string. For example, I...
11
by: name | last post by:
Here is a first attempt at a line/word wrapping utility. Seems to work okay, but lacks some checking stuff, etc. --------------------------------------------------------- #include <stdio.h>...
5
by: name | last post by:
Back for more critique. ---------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #define MAX 10000
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
2
by: srikar | last post by:
Hi can any one expalin me this piece of code In this code line is used for taking the input stream as line input stream. Please explain me Can I use getline() instead of line. When I am using the...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.