C++ :: Fscanf And Verifying Correct Input Length

Jan 24, 2012

I am trying to make a program that will read in input in the following format:

| ...text... || ...more text... || ...still more text...|

i.e. every bit of "text" that gets read in is bracketed on either side by the "|" character.

The buffer that i read them into can be of a maximum length of 101 (100 characters plus a terminating null character); this is how I am currently reading in the input:

char readString [100 + 1];
while ( fscanf(infile, "|%1000[^|]|", readString) ){
/*... manipulate readString ...*/
}

How might I design this program so that, should any of the text appearing between the "|" characters exceed 100 in length, it exits and returns a warning?

View 4 Replies


ADVERTISEMENT

C++ :: Delete Invalid Input Until Correct Input Is Entered?

Mar 20, 2014

Program is to add two fractions together like so..

This program adds fractions. ‘Y’ continues, any other key exits program
===================================================
Enter numerator 1 ==> 1
Enter denominator 1 ==> 3
Enter numerator 2 ==> 1
Enter denominator 2 ==> 6
1 1 1
--- + --- = ---
3 6 2
-----------------------------------------------------
Continue? Y or N! ==> n

My question is that i need each input to be a number.
if a character is entered i want it to output something like..

This program adds fractions. ‘Y’ continues, any other key exits program
===================================================
Enter numerator 1 ==> 1
Enter denominator 1 ==> a
You need to enter a number here.
Press any key to continue.

and then clear the part where the letter was entered.

This program adds fractions. ‘Y’ continues, any other key exits program
===================================================
Enter numerator 1 ==> 1
Enter denominator 1 ==>

View 5 Replies View Related

C/C++ :: Output Is Not Correct - Squaring Input?

Feb 6, 2015

Ok so This is what the output of the program should be.

enter the count of input integers: 1
The count of the integers must be two or more. Please enter a valid number : 3
Enter integer #1 : 2
Enter integer #2 : -1
Enter integer #3:5

The positive numbers entered and their squares are:

2 4
5 25

My Code :

//header files
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>

[Code].....

View 6 Replies View Related

C Sharp :: Input String Was Not In Correct Format

Apr 28, 2012

List<Byte> Pt_split(string plain)  {
            List<Byte> pb = new List<Byte>();
            // Byte p;
            string p;
            int temp = 0;
            if (plain.Length % 2 != 0)

[Code] ....

View 2 Replies View Related

C :: Unable To Input Correct Form For Matrices Multiplication

Jan 8, 2015

I am unable to input the correct form for matrices multiplication. I have an exam tomorrow in which I need to use this.

Code:

#include <stdio.h>
#include <conio.h>
int main() {
float a[10][10], b[10][10], c[10][10];
int i, j, k, l, n=0, m=0, x=0, y=0, sum=0;
printf("Enter the number of rows and collumns of the first matrix");
scanf("%d %d", &n, &m);

[Code]...

View 7 Replies View Related

C++ :: Correct Answer From File - Cin Getting Wrong User Input

May 31, 2014

So I'm writing a program that will read a text file and it will do certain "functions" the text file will be like a game, and the program will be able to run many different games. The problem i'm having is in one of the "functions" i believe it's the one meant to get a correct choice from a file and a user input and put them into strings, it gets the wrong input. I know this because i tested both strings the "function" writes too. it gets the correct file answer but the user input is always "c" for some reason.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int progfiles() {

[Code] ....

This is the code, most likely the section that is surrounded with "///" contains the problematic "functions." This is the command Line output...investigate why they're dieing or continue on your journey?(i-investigate/c-continue) got input and answer

i
c
i
Press any key to continue . . .

The got input and answer are tests to make sure it got both, the first 'i' is my input, the 'c' is what it says my input is, and the 'i' is the correct answer from the file. this is the part of the file that the program reads to get that output

"1 Do you want to go investigate why they're dieing or continue on your journey?(i-investigate/c-continue) 2 8 i 7 3 4 You continue on picking up speed to get to the egg first.. and.. Oh No, You accidentally bumped into the walls! 2 12 5 1 You go over, and as you realize the walls are acidic and that's why the other sperm are dieing.. 2 6"

View 3 Replies View Related

C :: How To Validate The Length Of User Input

Oct 24, 2013

For the following program, I'd like to make sure the user input is between 1 and 40 characters. To do this, how would I complete the if statement below?

Code:
char p_input[41];
printf("Enter a name (1-40 characters):");
fgets(p_input, 41, stdin);

if (p_input ....??

View 6 Replies View Related

C/C++ :: Find Length Of First Sentence In Input String

Feb 13, 2015

I have a question about finding the length of first sentence in an input string.

For example, let the input string be: dream in code. community learning

The length of first sentence is 13 (blanks are included). My question is how to create conditions for multiple punctuation signs (!,?)? If while loop goes like:

while((str[i]!='.')||(str[i]!='!')||(str[i]!='?'))

it gives me an error for infinite loop.

Code:
#include<stdio.h>
int main() {
char str[100];int i=0,br=0;
printf("enter a string:");
gets(str);

[Code] ....

View 1 Replies View Related

Visual C++ :: User Input In Two Arrays - Comparison Always Show Zero Correct Answers

Nov 3, 2012

The following program takes user input into two arrays and should then determine how many items are different by comparing them with a loop. The comparison always show zero correct answers.

#include <iostream>
using namespace std;
const int QUESTION = 20;
//class for testing grades
class TestGrader {

[Code] ....

View 4 Replies View Related

C++ :: Input Number During Runtime - variable Array Length

Nov 28, 2013

I want to ask for a number as an input during runtime and then create an 2-dimensional array of size as specified by user. i.e. if the user inputs 3, the array should be of size 3X3, and likewise...

View 9 Replies View Related

C++ :: Input Length / Width And Height For 3 Boxes - Sum And Average Volume

Dec 6, 2013

How would I make it so that I can have someone input the length, width and height for all 3 boxes and then have it output the sum and average volume? Here's an example of what I would like:

INPUT - Enter Box 1 (Length, Width, Height): 10.1 11.2 3.3
INPUT – Enter Box 2 (Length, Width, Height): 5.5 6.6 7.7
INPUT – Enter Box 3 (Length, Width, Height): 4.0 5.0 8.0
OUTPUT – The sum of the volume is 812.806
OUTPUT – The average volume is 270.935

Here's my original code:

#include <iostream>
using namespace std;
double box(double length, double width, double height); // use double data
int main() {
double sum;
sum = box(10.1, 11.2, 3.3) + box(5.5, 6.6, 7.7) + box(4.0, 5.0, 8.0);

[Code] .....

View 1 Replies View Related

C++ :: Verifying Loop In A Linked List?

Aug 29, 2013

I was trying to implement Floyd's cycle finding algorithm to find loops in linked list. I implemented my code using 2 pointers but I am not able to figure out why my output is always is 0 even though a loop exists in the code.

#include "iostream"
using namespace std;
struct Node{

[Code]....

View 5 Replies View Related

C++ :: Verifying If The Number Is Special And Not Scary

Feb 22, 2012

Code:

Exercise. 1.5 A number is special if it is divisible (no remainder) by 15. A number is big if it is greater than 999. A number is weird if it is divisible by 5 and 6 but not 18. A number is scary if it is big or weird.

Write a program to check which of the following, 450, 540, 600, and 675 are special but not scary.

Declare four variables called special, big, weird, and scary and make suitable assignments to these variables as a number is tested.

I want to do it with a function, but it just output that everything is Special and Scary, and that everything values 4199536.

Code:
#include <stdio.h>
#include <stdlib.h>
/* Function */
int test(int);
int test(int a) {
int sc, sp;

[Code] .....

View 5 Replies View Related

C :: Malloc - Verifying Amount Of Memory Allocated

Sep 7, 2013

How can I view the number of bytes that have been allocated by using the malloc function?I tried:

mem = (float*)malloc(num*sizeof(float));
printf("The amount of memory allocated using malloc is %d.", mem);

Note: The variable "num" in my program is equal to 7.But every time I run the program, this value changes.

View 10 Replies View Related

C :: Fscanf Data And Delimiters

Dec 7, 2013

How would I fscanf this piece of data?

Delimiter is --> :
VS1234567890654327:Rob Fordfirst:001:200
VS1234567890654312:Steven Harper:200:010

Code:
while(3==fscanf(filename, "????", &string[size], &name[size], &number1[size], &number2[size])) {
//printf("%s - %s - %.3d - %.3d", string[size], name[size], number1[size], number2[size]));
size++;
}

View 6 Replies View Related

C :: Fscanf Data To Arrays

Apr 17, 2014

I am a very fresh c programme user, in fscanf data to arrays. Somehow it just doesn't gets recorded, wonder what is wrong with my code? When I printf the array it always ends up with random numbers.

Here is my code:

#include <stdio.h>#define SIZE 2
#define count 2
int main()
{
[code]....

and the file i am trying to read from :

Code:
1, 2, 3, 4
5, 6, 7, 8
8, 9, 10, 11

I am trying to scan a list of 3 by 4 numbers and the 4 numbers of each column to the 4 different arrays, so if I read from the data above, i get an array with numbers 1,5,8; another with 2,6,9 and so on. Is it how its done in my code?

View 2 Replies View Related

C/C++ :: Loading File With Fscanf?

Apr 21, 2015

I am trying to copy the stats from the save file "lol.txt" to main. I posted what the contents of the file below, but for some reason the I get for output jiberish and I cannot seem to figure out why.

#define _CRT_SECURE_NO_WARNINGS
#define MAX_NAME 31
#include <stdio.h>
int main(void){
char sName[MAX_NAME];

[Code] .....

View 5 Replies View Related

C :: How To Count Lines In Dat File Using Fscanf

Sep 15, 2013

I need a way to count lines in a .dat file using fscanf. I must use a regex to check that the characters in the file are alphanumeric. In essence I need to count the ' ' character. I know fscanf ignores this character. I need to exit if it detects anything other than alphanumeric with the digit that is "problem" along with the line number. My .dat file is:

Code:
howard jim dave
joe
(
Maggie My fileCheck.c is: Code: #include "main.h"
int fileCheck(FILE *fp)
{
int ret_val;
int line_count = 0;
char file[BUFF];

[Code]...

The output I am getting is:

Code:
file opened Digit: ( is not an alphanumeric character on line: 5 Program will exit! File closed As you can see, the character "(" is not on the 5th line but the 3rd. It is the 5th "string."

I also wanted to show the regex I am using.

Code:
#define to_find "^[a-zA-Z0-9]+$"

How this can be accomplished?

View 5 Replies View Related

C/C++ :: Use Fscanf To Obtain A Set Of 14 Or So Strings Per Line?

Feb 25, 2012

I am trying to use fscanf to obtain a set of 14 or so strings per line, in a line where there are around 80 or so different sets of strings. I only need the first 14 and whenever I call scan f it starts at column 209 as opposed to column 1 where it should. Here's a sample of the code:

FILE *d;
d=fopen("t.dat","rb");
where a, n are all strings.
fscanf(d,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s",&a,&b,&c,..(etc)..,&n);

A sample of the Input file looks like this:
USB270.15385-29.63146 270.153847 -29.631455 2.966699e+03 -9.99 1.300391e+03 -9.99 -9.99 A-A-- 6.787463e+01 -9.99 1.555773e+02 -9.99 -9.99 10100 | ----- ------ ------ ------ | 0.373 13.554 12.928 12.670 AAA | ----- -------- - -------- - -------- - -------- - | --- ---------- - ---------- - --------- - --------- - --------- - ---------- -

View 7 Replies View Related

C :: Program With Fscanf Keeps Crashing - Using Eclipse Kepler

Oct 13, 2013

So this program I have to make keeps spitting out an error report when I try and run it. I am using eclipse C kepler.

The file it is supposed to read has the following text;

R1 N001 N003 20
R2 N002 N001 5
R3 N001 0 10
R4 N002 N003 10
R5 N003 N000 5
I1 0 N002 10

View 2 Replies View Related

C :: Fscanf - Segmentation Fault For Larger Project

Apr 19, 2013

I added this snippet of code to a larger project I use to read some numbers in from a file, but for some reason I'm getting a segmentation fault.

Code:
printf("starting main
");
FILE *cutoffs;
double cut1=0, cut2=0, cut3=0;
printf("trying to open file

The cutoffs.in file looks something like

Code: 3.475 3.875 4.025

I ran the gdb debugger, so I know it is occurring at that fscanf; however, it doesn't give any other details (to be honest I don't have much experience with debuggers :x) ..The values are being put into an array. Before I just had numbers initialized in the array by hand, but need to have 20 different runs of this code with different numbers each time...

View 2 Replies View Related

C :: Fscanf Data To Array - It Always Ends Up With Random Numbers

Apr 16, 2014

I am a very fresh c programme user, and need to fscanf data to arrays. Somehow it just doesn't gets recorded, wonder what is wrong with my code?

When I printf the array it always ends up with random numbers. Below is my programme which is just designed to read a doc and print it back out, as well as the text doc it is designed to read.

View 3 Replies View Related

C/C++ :: Read Name / Phone Number And Address From A File - How To Use Fscanf

Nov 28, 2014

I am trying to read the name, phone number and address from a file ... Here is the code but it doesn't work and i don't know where is the problem ...

#include <stdio.h>
#include <stdlib.h>
struct PhoneBook{
        char name[50];
        int phoneNumber;
        char address[50];

[Code] ....

And here is the contents of the file:
jack 01014 jgd

Moh 02925 Tyeu

When I run it on dev c++, a message appear saying that the prog has stopped working and no output appears. I want to put the name in user[n].name, the phone number in user[n].phoneNumber and the address in user[n].address

View 1 Replies View Related

C++ :: Get Correct Answer For Every Value Of X / N And A

Jun 10, 2014

I am expecting to get correct answer for every value of x, n and a[i] where

-10^9 <= a[i] <= 10^9
1 <= x <= 10^18
and 1 <= n <= 10000

Every value here is an integer value

#include<cstdio>
#include<iostream>
#include<cmath>
#include <float.h>

#define sBig long long int

[Code] .....

View 1 Replies View Related

C++ :: How To Correct The Search In The End Of Program

Jun 1, 2013

Code:
#include<conio.h>#include<iostream.h>
class air_line {
private:
char name[5][100],name1;
long double opt,opt1,opt2,cnic[5],ticket_number[5],ticket_number1,cnic1;
public:
void input()

[code].....

View 1 Replies View Related

C :: Correct Answer Does Not End Game

Feb 21, 2013

Okay so I thought I had this assignment completed properly last week. Last night I found a bug while playing the game.why won't the game end when the player guesses the correct number? The game allows you to finish using the max number of guesses even though you already guessed the correct number.

Code:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAXGUESSES 6
#define MAXGAMES 20
#define MAXNUMBER 100
}

[code]....

View 9 Replies View Related







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