C/C++ :: Program To Scan Number And String Then Print Them To A File
Mar 19, 2015
I wrote this program to scan a number and a string until EOF then print them to a file named "data.list". the problem is that the program duplicates last line of input in the output file. so for example if the input is :
1 test
2 dream
3 code
then output (in data.list file) would be:
1 test
2 dream
3 code
3 code
I also changed the program code so that it reads from data.list file. even here it duplicates last line!
so when program reads the info above saved in data.list it would be:
1 test
2 dream
3 code
3 code
3 code
here's the code for writing:
#include <stdio.h>
int main( void )
{
int num;
char str[80];
FILE *fPTR;
fPTR = fopen( "data.list", "w" ); // opens a file named "data.list", create if doesn't exist
while(!feof(stdin)) // loop until End-Of-File is pressed. Ctrl-Z in Windows, Ctrl-D in Unix, Linux, Mac OS X
[Code]...
and the one for reading from file:
#include <stdio.h>
#include <conio.h>
int main( void )
{
int num;
char str[80];
FILE *fPTR;
[Code]...
How do I fix this behavior??
View 3 Replies
ADVERTISEMENT
Apr 10, 2013
I have to create a small data base for a shop. One of the functions i am creating is taking a customers ID and scanning that through a text file and to print out the info about that customer. What i am having trouble with is where do i insert the string compare in my program?
//declaring array for input of customer ID
int customer_ID [20];
printf("Please enter the customer ID:");
gets( customer_ID ); //users input stored in the array
[Code] .....
View 2 Replies
View Related
Jan 28, 2013
This is my code:
int main() {
int num;
printf("Please enter a number: ");
scanf("%d", num);
printf("%d", num);
return 0;
}
when i compile and run it, it stops working and doesn't printf the integer.
View 3 Replies
View Related
Nov 3, 2013
I need to write a program that will allow the user to enter a number "n" and the program tell you that the nth prime number is .....
EXAMPLE
user enters 55
printf("The 55th prime number is %i", variable");
View 1 Replies
View Related
Feb 3, 2015
I need a code that will search through string 1 and find the first place with a letter that also appears in string 2 and return the pointer of that place. This is what I wrote:
char strPbrk(const char *s1, const char *s2) {
char p = *s1;
for (int i = 0; i < strlen(s1); i++)
for (int j = 0; j < strlen(s2); j++)
if ((p+i) == *(s2+j))
return p;
return NULL;
}
but it continues to return wrong values idk what I'm doing wrong.
View 4 Replies
View Related
Sep 22, 2013
Write a program to print out the binary value of a 16 bit number.
Create integers i, count, and mask.
Set 'i' to a hex value of 0x1b53.
Set mask to a value of 0x8000. Why?
print a line to show the hex value of i and then the leader for the binary value like this: Hex value = 1b53 Binary=
Use a for loop to loop 16 times and print 16 digits, using count as the loop counter
To test for each digit value, bitwise and 'i' with 'mask'
when the result for the bitwise and is true, print the number '1'
when the result for the bitwise and is false, print the number '0'
then shift mask one place to the right
print a new line and then quit
Use prtscrn and make a hard copy of the code with the console output.
Extra: use the modulus of count and print a space after every 4th digit to make the binary easier to read
The output should look like this: Hex value = 1b53, Binary= 0001 1011 0101 0011
so far this is what i have
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, count, mask;
// 1B53 0001 1011 0101 0011
// 8000 1000 0000 0000 0000
i = 0x1b53;
[Code] ....
it is telling me that there is an "else" without previous "if", also is the program that I wrote correct?
View 3 Replies
View Related
Jul 27, 2014
This program prints out the total number of up to 100,000. How to do is to print out the number of twin primes up to 100,000. Is it as simple as adding an if statement "if(primesList[j] - i == 2)"?
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int primesCount = 0;
[Code] ......
View 4 Replies
View Related
Nov 11, 2013
how to make a programm in native c++ which print out all prime numbers less than or equal to given number????????
View 5 Replies
View Related
Oct 20, 2013
Well what the title says, but I can't get it done. This is what I got
Code:
#include <stdio.h>#include <string.h>
int main(void)
{
char word;
int count;
printf("Enter a word.
[Code] ....
View 4 Replies
View Related
Nov 24, 2014
I'm trying to make a program in C where the user enters a string and it prints a word for example (name) in lowercase then the same word again but in capitals and lowercase like this (NnAaMmEe).
my code is below;
#include<stdio.h>
#include<ctype.h>
int main()
[Code].....
View 1 Replies
View Related
Feb 18, 2015
Basically this is what i need to do. Write a program that reads a number from the keyboard, separates it into its individual digits and prints the digits to screen, each on its own line followed by the same number of stars as itself.
For example, if the number is 2339 the program should print
9 *********
3 ***
3 ***
2 **
So far i have managed to separate the number and have them on different lines, but how to implement the stars onto each line with the number!
My code so far:
int main() {
int n;
printf("number? ");
scanf("%d", &n);
while (n > 0) {
printf("
%d
[Code]...
View 4 Replies
View Related
Mar 24, 2014
write a program in c to read a sequence of N integers and print the number that appears the maximum number of times in the sequence.
View 1 Replies
View Related
Feb 20, 2013
How to make it so an output for a program would print out the string in reverse/backwards? I was just curious on how to do this.
View 6 Replies
View Related
Jan 29, 2012
Write a program that prompts the user for the name of a file. Then it opens the file, and counts the number of words and lines in the file, and prints out those counts.
I think I possibly could somehow use a counter to increment using getLIne() until getLine() returns NULL, but the problem is, I'm worried if I try that, a file that looks like this:
Bla bla bla bla bla lkfdljkfaklafdskjladsjkdfkjlkdfjdfshafdsjkjrerjkkjfaddjkfsafkjdjakdfsjkasfjkjkfdskjldfjkfjkdjfkdsakdjfkjfdkjdfskjfdsk
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjdfkerea
blkjadkjlfdskjldfkjlfdkjfdjkdfsjkldfskljfksfdljfd
Only register four lines and not get the one after the fourth line.
So I was thinking of something like
Code:
#include <iostream>
#include<ifstream>
#include<cstring>
#include<string>
#include<vector>
#include <cstdlib>
using namespace std;
iostream keyboard;
ifstream file;
iostream cin;
[Code] .....
Did I do that right? Will that count the number of words and number of lines correctly?
View 14 Replies
View Related
Mar 4, 2013
This is my text file.
bak kut teh[tab]888.0[tab]989.0
spicy chicken nugget[tab]999.0[tab]978.0
I'm experiencing some problem in trying to scan and put it into an array for the food names (e.g an array for food which consist of bak kut teh and spicy chicken nugget) and another 2 array for the other individual integer into C. However there seem to be some problem with my code.
Is there a difference if I use both tab and space instead of space for all?
Code:
#include<stdio.h>
int main() {
FILE *fp;
char food[100];
char buff[BUFSIZ];
float num;
[Code] ....
View 2 Replies
View Related
Oct 4, 2013
I'm working on a silent auction program that will scan a file and find the highest from each group of bids, then have a running total of money made throughout the auction. I'm pretty sure the rest of my code works, i'm just getting stuck on finding the largest number from the line in the file, saving it, then moving to the next auction.
input file text (first number is num of auctions, after that it's num of bids, then the bids):
5 4 100 500 250 300 1 700 3 300 150 175 2 920 680 8 20 10 15 25 50 30 19 23
Sample Output
Auction 1 sold for $500.00!
Auction 2 sold for $700.00!
Auction 3 sold for $300.00!
Auction 4 sold for $920.00!
Auction 5 sold for $50.00!
The silent auction raised $2470.00 for charity!
Code:
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
[Code].....
View 4 Replies
View Related
Aug 23, 2013
Code for the following.
How to print alternate records from a file using fseek?
View 5 Replies
View Related
Sep 1, 2014
is there a to use printf to print certain number of blank spaces, where the number is derived from the output of a function?
for(m=low; m<=high;m++)
{
printf("t=%2d %'f(m)-1's %3c", m, ' ',*);
}
This is suppose to output
t=-3 *
t=-2 *
t=-1
.
.
.
View 2 Replies
View Related
Mar 14, 2013
Objective of this program is to read binary data from a file and print it.
Code:
#include <stdio.h>
void readFile(void);
int main(){
readFile();
return 0;
[Code] .....
But When I Run This Code First It Print "Error." Then Rest Of The File.Say In My File I Have "I AM HUMAN", It Prints "Error. HUMAN"!!
I Cant Figure Out Whats Wrong In The readFile() Function.It seems right to me.
View 4 Replies
View Related
Feb 20, 2015
I have a text file which contains many sentences. I am trying to extract only the numerical values from the string that conatins characters,numbers and white spaces from the file. I want to seperate the characters and numbers into different parts.
for example: the file contains sentances as given below.
I have to go to school for 10min.
You will come here in 15min.
He stayed here for 20min.
from the above sentances, I want to seperate " I have to go to school for " and "10" and put them into two different variables and also 10 should be in integer format.
View 1 Replies
View Related
Nov 24, 2013
How to reserved int number and print as float number ?
View 1 Replies
View Related
Apr 25, 2014
I am reading a file whick looks like following:
Code:
10 = NAND(1, 3)
11 = NAND(3, 6, 5)
15 = NAND(9, 6, 2, 8)
Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?
My code:
Code:
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))
{
std::size_t guard_found = line.find(guard_str);
[Code].....
View 8 Replies
View Related
Oct 9, 2014
In this code, i declared a string constant and trying to print the length of string. I know that if i write char a1[7] or char a1[] than it runs and give aggregate output, but in this case it is giving double length of string.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
}
[code]....
View 5 Replies
View Related
Jan 16, 2014
I've been working on a program that displays the number of digits in each line of a file, but I feel stuck. Take for example a file that contains these characters:
6347aaa9
54j
811111
6a
709
And I'm trying to display a result like this
1 //that's the number of the line 5 //the number of digits
2 2
3 6
4 1
5 3
Here's what I've written so far:
Code:
#include<stdio.h>
int main() {
char a=0;
int number_of_digits=0, linescount=0, num, number_of_digits_per_line=0;
FILE *inputFile;
if(!(inputFile=fopen("C:TestTest.txt","r")))
[Code]..
I also thought of using fgets and strlen but I am not very good with them and couldn't get the program to work correctly. It did work but it displayed all characters, letters included, not only digits.
View 2 Replies
View Related
Feb 3, 2013
I need to create a program that asks the user for the filename, then counts the number of occurrence of each letter in that file.
Ex. if the file contains
Absacsac
asdasda
Output will be
a = 6
b = 1
c = 2
.
.
.
z = 0
This has been my program so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include <cstdlib>
void countingFunction(string sentence) {
[Code] .....
View 2 Replies
View Related
Jan 25, 2013
I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z). The pattern of the source text file is like this:
known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089
Until now I could manage to split the files, but the output gives me only zeros. First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.I've got for the three .txt files columns like this:
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
View 2 Replies
View Related