C :: Counting Function - Prints All Input Lines That Are Longer Than 80 Characters

Jan 10, 2014

I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time.

Here is my full code:

/* Exercise 1-17 Write a program to print all input lines that are longer than 80 characters */

#include<stdio.h>
/* Declarations*/
#define MAX_STRING_LEN 1000
int count_characters(char S1[]);
int main() {

[Code] .....

So I was trying to debug my count_characters() function and this is the code if I was to run it seperately:

Code:
#include <stdio.h>
/* counts character of a string*/
main() {
int nc = 0;
int c;
for (nc = 0; (c = getchar()) != '
'; ++nc);
printf("Number of characters = %d
", nc);
}

which works...

View 1 Replies


ADVERTISEMENT

C++ :: Counting Characters / Words / Lines And Digits

Mar 20, 2013

I am writing a program for my class in C++. For this program we are required to use different fuctions and prototypes outside of main. We have to determine the number of characters, lines, sentences, digits, words, etc. in a particular function the user types in.

View 1 Replies View Related

C :: Working With File And Counting Characters / Words And Lines

Dec 17, 2013

I have to write a program (on linux) which will count character, words and lines like wc linux command. I'm trying to write this for last 3 days... First part of app I did and it works fine - command line options to choose. Then I've got a function read_file which I have to use to read a file. One of the options is to get the file name from user and if user will not type any name then the standard file is ubuntu dict file /usr/share/dict/words, this is not working as well...

Counting characters and lines is working fine but because I don't know how to get text from read_file wrote code to read file interior this functions. Words counting is working partly - everything is fine until there are two or more spaces, tabs one after another then counts extra words. Finally I need child processes in words and lines counting functions. Parent process should waits for all childs to finish and should be pipes to submit character counts back to parent process. How to do all this things with processes...

Code:

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
/*size of character buffer to read in a file. */
#define BUFFSIZE 1000000

[code]....

View 2 Replies View Related

C++ :: Giving One Line Command That Prints Lots Of Lines - Ostream?

Nov 9, 2012

I'm using cout to print lots of lines. But i want to put together them in one variable etc. Then , i want to print it. I think i can do it with ostream but I cant do it . Is there any example use of ostream.

I want to use it such that:
x<< "hello" << endl;
x<< "mike" << endl;
x<< "how " << endl;
x<< "are" << endl;
x<< "you" << endl;
cout << x;

Will print

hello
mike
how
are
you

How can i do it?

View 5 Replies View Related

C++ :: Counting Lines From A File

Apr 23, 2013

How I would go about counting lines from a file at the same time as extracting words? I need to know the line number to output what line the word is misspelled on. I tried getline and using sstreams but I could not get it to work.

void SpellCheck::checkValidWords(const string& inFileName) {
string eachword;
ifstream istream;
istream.open( inFileName.c_str() );
if ( !istream.is_open() ) {

[Code] .....

View 3 Replies View Related

C :: Counting Number Of Lines In A File

Feb 2, 2015

it looks like a popular method for determining the total lines in a file is to read the entire file character by character in search of ' '. I have a file with 5 lines, but for some reason this code isn't finding any instances of ' '. Is this possible? Is there a better way to get the number of lines in a file?

The file looks like this:

Code:
NAME: John
FRIEND 1: Steve
FRIEND 2: Andrea
FRIEND 3: Ken
OCCUPATION: Programmer

Code:
FILE *f = fopen(currentFile, "r");
if (f == NULL) perror ("Error opening file.
");
int ch, lines = 0;
while(!feof(f)){

[Code] .....

View 2 Replies View Related

C++ :: Counting The Number Of Lines In A File?

Jun 3, 2013

I was trying to make a function that would return how many lines are in a certain file. I was looking at [URL] which talks about how to count how many characters are in a text file. I just want the number of lines with in a file.

View 2 Replies View Related

C/C++ :: String Prints Unusual Characters?

Sep 10, 2014

The print statement below I commented prints out unusual characters. I have two strings from the command line that I pass to the TKCreate function. Inside the function the strings print out fine, but when I return a pointer to the struct back to the main method one of the strings prints out fine, but the other one prints out unusual inconsistent characters.

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

[Code]....

View 10 Replies View Related

C++ :: Counting Number Of Lines Inputted By User (File Stream)

Feb 12, 2014

So I'm trying to count the number of lines in a text file that is inputted by the user. Also the code doesn't sum up the last number of the text file (exmp it calculates and print only 14 from 15 numbers). I'm a beginner in c++ programing.

View 3 Replies View Related

C++ :: Declaring Arrays - Counting Number Of Lines In Text File

Feb 9, 2013

I'm creating a program to read information about class schedules at my school, reformat the information, and allow the user to search for specific semesters. There are eight fields of information. I'm reading the info from a text file using eight parallel arrays, but I'm having trouble declaring the arrays. I can run this code in one compiler (Dev-C++) with no problems, but I get errors when trying to compile it using Visual Studio stating that arrays must be declared with a constant value. I have a loop to run through the text file, with a counter to increment with each subsequent line, then I create a constant int equal to the counter, and declare the arrays of size equal to the constant int. Here's the section of code in question:

// Counting the number of lines in the text file
inFileForLines.open("CIS225HW1DA.txt");
string countLine;
int numberOfLines = 0;
//Discarding the first line of the text file containing only column headings
getline(inFileForLines, countLine);

[Code] .....

View 7 Replies View Related

C++ :: Counting The Number Of Characters In TXT File

Feb 22, 2015

I have recently hit a stump with C++ and have been getting pretty frustrated with this assignment. I just can't seem to find out how to start the assignment. Basically for the first part of the assignment, I need to find the number of characters in this .txt file my teacher provided for me. The only exception is that I can only count the actual letters themselves, not spaces or punctuation. How would I go about doing this? I have read about functions like isalpha but can't figure out on how to fit them into code to do the first part of this assignment.

View 2 Replies View Related

C/C++ :: Counting Either Vowel Or Consonant Characters

Oct 20, 2014

So I've done this program that's supposed to count how many vowels or consonants you enter, depending on which one you choose, and also count the total number of characters you've entered. But it's not outputting the correct answers. The current output that it's showing seem to me that it's going out of bounds or something.

#include <iostream>
#include <cctype>
using namespace std;

[Code]....

View 3 Replies View Related

C/C++ :: Counting Characters In A String / File

Jan 19, 2015

I have the following program

#include "stdafx.h"
#include "string.h"
#include "ctype.h"
/*selection sort*/
void swap(int A[], int j, int k) {
int p = A[k];

[Code] ....

Theoretically it should cound the number of apperances of each character in a string, but:

1) It is counting characters from letter "j" upwards thus all asci characters below "j" are not counted, why is that?

2) I want also to make this code calculate the number of apperances of each character in a txt file. How to do that, i know i have to open a file in read mode, that's all (I am new to c programming)?

View 14 Replies View Related

C :: Counting Number Of Characters Copied To File

Jul 21, 2014

I'm trying to copy a file into another file and copy the number of characters copied but my while loop doesn't even enter into a loop indicating the file is already at the EOF file character. I've confirmed this by placing the printf() statement inside the while loop, which doesn't print anything and by keeping it out of the while loop and changing the chars_copied to something like 9, it prints 9 for number of chars_copied. I don't understand why the file is already at the EOF character, I've tried this with a few more files, it's the same result.

Code:
#include <stdio.h>
int main(void){
FILE *input_file, *output_file;
int c, chars_copied=0;

if((input_file=fopen("C:workmarks.txt", "r"))==NULL)
perror("input file open failed");

[Code] ....

View 13 Replies View Related

C++ :: How To Include Spaces In Counting String Characters

Jun 16, 2014

So i have this program that's supposed to count all the characters including the spaces without using strlen but it only counts the number of the first word's characters and it does not include the rest of the words after the first space. how do i include spaces in the count?

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

[Code].....

View 5 Replies View Related

C++ :: Finding Number Of Lines Between Two Specified Characters

Feb 3, 2014

I am looking for a way to correctly count the lines between two specified characters/strings in a file. Here's the part I need work on:

getline( file, lines );
do {
if(lines.find("character")
{
++counter;
}
} while( !lines.find("story") );

I want the code to search for the first occurence of the word "character," and start counting the lines from that line until it hit the first occurrence of the word "story."

Right now, I am only getting a counter value of 1.

View 2 Replies View Related

C++ :: Read A File And Break It Into Lines And Characters?

Jan 31, 2014

I am writing a code to read a file and break it into lines and characters but I am getting an error do not know the reason for it though.

here is my code

#include<fstream>
#include<string>
#include<iostream>

[Code].....

The first line is executing properly but when it goes to the second line I get an error "the program has stopped working"

View 1 Replies View Related

C/C++ :: Reading Lines Of Numbers / Characters While Checking For EOF?

Aug 30, 2014

Simple program to convert files to XML files. However I am trying to narrow down why my code is not working. I am reading a line that looks like "2001 Joe Dirt Los Angeles", then the next few lines are followed by text and the the fifth line looks like "---End of Description---".

So here is the file

2001 Joe Dirt Los Angeles
Home is where you make it
Best movie ever(but not really)
But seriously
---End of Description---

Here is example code to test the process

string line;
while(dataFile >> line) {
getline(dataFile, line);
cout << line << endl;
}

My returning output is

[space]Joe Dirt Los Angeles
[space]Home is where you make it
[space]Best movie ever(but not really)
[space]But seriously

My test code is skipping the date on the first line but replacing it with a space. Then it is skipping my fifth line which reads "---End of Description---". Now if I take the code out of the while loop and hard code in five lines of output (x5 getline(dataFile,line); cout << line << endl;) then my code works as expected. I get all of the information. Date and the fifth line.

It looks like when I am testing for the EOF it is also taking in the numeric value at the beginning of the first line. However, that does not explain why it is dropping that fifth line.

View 3 Replies View Related

Visual C++ :: Looping Lines And Blocks Of Characters

Mar 17, 2013

I have a file that is like the following, with patterns of pipes in it:

Code:
||| || | |
| ||| | | |
|| || ||

I have to consider each row of pipe characters to be in blocks of 3 characters each (e.g. - positions 1-3, then 4-6, etc, etc...) but I have to capture all of the pipes, in sequence, like so:

positions 1-3 for lines 1-3, then positions 4-6 for lines 1-3, etc, etc...

How to get this done besides writing severely redundant control structures like loops, one after the other?

View 5 Replies View Related

C++ :: Change Making Program Prints Correct Values But Prints Them In Loop

Jan 27, 2015

I have a program that makes change from an amount. It works fine, but the output is the correct output looped over and over. I have tried everything, but it still doesn't work. For example, a amount of 98 should print

3 quarters
2 dimes
0 nickles
3 pennies
but instead it prints
3 quarters
2 dimes
0 nickels
3 pennies
0 quarters
2 dimes
0 nickels
3 pennies
0 quarters
2 dimes
0 nickels
3 pennies

Why it's doing this?

Code:
#include <iostream>
using namespace std;
int coinscount(int& amount, int value) {
int tracker = 0;
int amountdimes = amount;

[Code].....

View 3 Replies View Related

C/C++ :: Create A Program That Prints A Certain Number Of Asterisks Based On User Input

Apr 12, 2015

I need to create a program that prints a certain number of asterisks based on user input. The user inputs 5 and I want my program to output "*****". How would I do this in C? I've tried printf("%#**", myvariable) but this does not work it only prints "*".

View 1 Replies View Related

C :: Reverse Function That Inserts And Prints Binary Function Correctly

Nov 3, 2013

I have written a function that inserts and prints a binary function correctly.

For example a tree like this [URL] ..... would print like this

Code:
node: 10
node: 7
node: 6
node: 8
node: 9

[Code] ....

Here is my print function

Code:
void prt_tree(struct node *tree) {
if (tree == NULL) {
printf("Null Tree
");
return;

[Code] .....

Could I just make some adjustments to my function to reverse it? and if so, how?

View 2 Replies View Related

C++ :: Program Prints Correct Result But Prints It Over And Over

Feb 9, 2015

I have a program where the user inputs a line of integers, and then all unique ones are outputted. It works fine-almost. It prints the numbers correctly, but prints them more than once and I'm not sure why.

Code: #include <iostream>
using namespace std;
int main ( ) {

[Code]......

View 10 Replies View Related

C++ :: Counting Vowels In User Input String

Feb 11, 2013

I need to write a program using at least one while loop to count and display the amount of vowels in a user input string. This is what I have so far.

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int acounter(0); // Create counters for each vowel

[Code] ....

View 2 Replies View Related

Visual C++ :: Vector Input / Counting Words

Feb 3, 2014

I am trying to write a program that counts specific words that a user inputs "Howdy/howdy/Whoop/whoop" (yes I go to Texas A&M, hence those specific words) I am having an issue where it wont count the first word even if it is "Howdy"

For example if I put it:

"Howdy howdy whoop Whoop" - it only outputs that it counted 3 words

now if I were to do:

"Hello Howdy howdy whoop Whoop" - it would count 4.

Code:

#include "std_lib_facilities_4.h"
int main(){
cout << "Please enter desired words, when you have entered all words, please type CTRL+d (EOF Command)
" << endl;
//It was assumed that EOF command was going to be used here hence the necessity of 'CTRL+d'
vector<string>words;

[Code] ...

View 2 Replies View Related

C++ :: Code Blocks No Longer Giving Any Useful Errors

Jun 8, 2013

I'm using Code:Blocks 12.11 on windows 7, with the built-in MinGW compiler. When I try and compile a program that has an error in it (misnamed variable, missing include, extra semi-colon somewhere, anything) instead of saying something about what went wrong it just has this:

Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)

Which is not particularly useful for debugging.

View 11 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved