C :: Reading Multiple Inputs Sscanf

Jan 23, 2014

I am beginner at C and I was working on a program where I have to read in a line such as Digit, String, Float. The string can have any amount of spaces between it. and each input is separated by a space character.

The input is of the format:

10000000000 hello my n ame is 30.2

So I used fgets(x,100,stdin) to read the line in.

So I need to read that line into an int, array, and float.. so I was thinking of using this:

sscanf(x, "%d %[^/n] %f", &number, &username, &numberfloat);

Now, obviously I can't use the /n to read in the username with spaces because I need to read the 30.2 into a float variable.

View 5 Replies


ADVERTISEMENT

C++ :: Using Cin That Accept Multiple Inputs

Feb 14, 2013

For a program I am required to use a cin that accepts 4 variables. The first describes a function such as add(), remove(), print(), or quit(). The problem is that to use add() I need to input all 4 variables but for remove(), only 2 variable input is needed.

I want the input to be "add 9 James Bond"
or be "remove 341"

Here is my current code.

int command(string command, int Id, string first, string last){
while (command != "quit"){
cout << "customers> ";
cin >> command >> Id >> first >> last;
if (command == "add")

[Code] .....

View 1 Replies View Related

C :: Processing Outputs Of Multiple Inputs

Nov 26, 2013

It's not something trivial but I would like to know the best way to process multiple outputs, for example:

Input
First line of input will contain a number T = number of test cases. Following lines will contain a string each.

Output
For each string, print on a single line, "UNIQUE" - if the characters are all unique, else print "NOT UNIQUE"

Sample Input
3
DELHI
london
#include<iostream>

Sample Output
UNIQUE
NOT UNIQUE
NOT UNIQUE

So how can I accomplish outputs like that? My code so far is:

Code:
int main(int argc, char *argv[]) {
int inputs, count=0;
char str[100];
char *ptr;
scanf("%d",&inputs);

[Code] ....

But the above will obviously print the output after each input, so I want to know how can I achieve the result given in the problem. Also another thing I want to know is, I am using an array of 100 char, which it can hold a string up to 100 characters, but what do I have to do if I want to handle string with no limit? Just declaring char *str is no good, so what to do?

View 5 Replies View Related

C++ :: Running Same Code With Different Inputs On Multiple CPUs

May 28, 2014

I have a C++ code reading large data from an input txt file, doing some calculation on the data, and writing the result of calculation in another txt file.

I have about 300 input files, and the calculation time for each input file is pretty long (~4 days on a single CPU), so I would like to run the same code on multiple CPUs for different inputs.

Which is the most appropriate strategy in this case, multithreading, mpi or something else?

View 4 Replies View Related

C++ :: How To Access Certain Elements And Store Multiple Inputs

Jan 20, 2014

am trying to create a program that asks the user personal questions.

std::vector<std::string> name, age, favsinger;
std::cout << "Hello, what is your Name Age Favorite_Singer? ";
std::cin << name; //i want to store the user's info along with the sibling's

[Code]....

View 4 Replies View Related

C/C++ :: Record Multiple Inputs For 2 Orders On 2 Different Registers?

Feb 26, 2015

The tools Hal's sells are: Hammers: $10.99 Wrenches: $10.99 Levels: $19.99 Tape Measures: $4.99 Screwdrivers: $8.99

In order to make things easier in the long run you have decided to make a c++ program that takes in all the orders for each register and tallies them at the end of the day.

Write a c++ program that does the following:

Create a Register class that can store all of the receipts for the day and the totals for that register.

This class could have the following member functions:

Contructor(int) - Creates a number of receipts based on an integer that is passed into the constructor (this is the largest number of orders that the register can take). You may also be required to create additional variables to make the program work but that's up to you to determine.

void getorder() - Asks the user how many of each item they want an stores that in the first available receipt.

void returnreceipts() - This function returns the details of all the receipts and the total for the day.

Register operator+(const Register &right)- This is an overloaded addition operator. When two objects are added together an empty temporary register is created (with 0 receipts) and only the totals of both objects are added to it. The temporary register is returned by the function.

Register operator=(const Register &right)- This overloaded assignment operator copies only the totals from the Object on the right.

In the Main function I would like:

Create 3 Register Objects. The first two registers are the ones in the store and should be initialized to take 10 orders. The third is used at closing time and can be initialized with 0 receipts as it will only take in the totals from the other registers.

Run the getorder function for registers 1 and 2 twice.
Run returnreceipts for register 1 and register2.
Reg3=Reg1+Reg2;
Run returnreceipts for register 3.

[Code]...

this is what I have so far I'm trying to get the input part working first, so the user would be asked each item individual how many they want? this will happen for 2 users, then the next register will do the same?

View 2 Replies View Related

C++ :: Reading Unknown Number Of Inputs And Adding Them In Vector

Jan 16, 2013

Consider the following piece of Code:

int ReadNumbers() {
int num;
vector<int> x;
cout << "Enter Numbers" << '

[Code] ....

The while loop is expected to terminate when the user provides an Invalid Input. But this while loop behaves unexpectedly when the user provides a 'Newline' input (by pressing Enter) and becomes an infinite loop. How can I prevent this from happening? Also I've heard that expecting invalid inputs isn't good code design. Is this true? If yes, then how can I solve my question without expecting Invalid Inputs?

View 10 Replies View Related

C :: Sscanf Causing Segmentation Fault

May 30, 2014

I have a problem in my code and after hours of looking I really cannot find out what is the problem.I use scanf to to parse lines of a file, let me copy only the important part:

Code:

/*File read...everything is fine*/
int seq_1, seq_i2;
int len;
while(fgets(&line[strlen(line)], line_length, file)!= NULL) {
}

[code]....

if (num_sc ==4) //the wanted format, do something So I am allocating all variables, and giving to sscanf pointers, so I really don't get where the segmentation fault appears.I checked with the debugger, the first line of the file is read, but at the second it crashes at the line of sscanf! The two lines don't have the format I want in sscanf but therefore I check if this number is 4 and then do other stuff to the data...

View 9 Replies View Related

C/C++ :: Sscanf Not Storing Variables Correctly

Jan 23, 2015

I'm trying to read in 3 values from the first line in a file. I want to take those 3 values and store them into 3 variables.

char buffer[100];
if((fgets(buffer, sizeof buffer, in) != NULL))
{
printf("current buffer is %s", buffer);
double tmpPrePrice, tmpDoorPrice;
int tmpTicCount;
int a = sscanf(buffer, "%d %d %d", &tmpPrePrice, &tmpDoorPrice, &tmpTicCount);
printf("applied %d variables %d %d %d", a, tmpPrePrice, tmpDoorPrice, tmpTicCount);
}

The input is

15 25 200

And the result:

current buffer is 15 25 200
applied 3 variables 15 0 25

any reason these aren't being stored properly?

View 3 Replies View Related

C :: Use Fgets And Sscanf To Read A Line Of Input

May 21, 2014

I'm new to C/C++. I am attemping to use fgets and sscanf to read a line of input, and confirm it is a positive number.My code works great, except for the case of a negative number. When I enter a negative number, my while loop seems to run infinitely, with stdin providng the same input over and over again.

Here's the code snippet:

Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEXT_LEN 64
void foo() {
char* inStr = (char*)malloc(sizeof(char)*TEXT_LEN);
memset(inStr, 0, TEXT_LEN);
}

[code]....

View 9 Replies View Related

C :: Reading File Multiple Times?

Apr 5, 2014

so i have to read a text file with an unknown number of lines and allocate memory to for the number of items in the text file.currently i read the entire file(counting the number of lines). i allocate memory according to the number of lines read and then use fseek() with an offset of zero to allow for the second read .

View 5 Replies View Related

C/C++ :: PugiXML - Reading Multiple Nodes With Same Name

Apr 4, 2014

<?xml version="1.0" encoding="ISO-8859-1" ?>
<packets_summary>
<item>
<index>1</index>
<frame_type>Ethernet</frame_type>
<local_mac_address>00-21-85-11-29-1b</local_mac_address>

[Code] ....

I am having trouble to read all attributes in all nodes called "item". I can load all attributes in one node "item" but I can't move to another node called "item " and load another attributes . I was trying a lot of different codes but I did not succsed.

for (pugi::xml_node node = doc.child("packets_summary").child("item").first_child(); node; node = node.next_sibling()) { cout << node.name() << ": " << node.child_value() << endl; }

This code just print all of the attributes in node called "item" but i need to move to another one and I really don't know how. I was trying to put into one for cycle another one but it did not work.

for (pugi::xml_node node = doc.child("packets_summary").child("item").first_child(); node; node = node.next_sibling()) { cout << node.name() << ": " << node.child_value() << endl; }

for (pugi::xml_node node = doc.child("packets_summary").child("item").first_child(); node; node = node.next_sibling()) {
cout << node.name() << ": " << node.child_value() << endl;
}

View 5 Replies View Related

C++ :: Reading Input From Multiple Keyboards And Mice

Apr 28, 2014

I wanted to know if there was a way to separate the inputs received if there were a few keyboards and mice hooked up to a desktop/laptop.

Would there be a particular class that would best be utilized to do this?

I read that MouseEventArgs is one class that can recognize events, but I'm not sure if its best for what I need.

View 2 Replies View Related

C++ :: Reading Multiple Text Files In Program

May 31, 2013

I want to know how to read multiple text files in this program... I have 5 text files(order1,order2,...order5).

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<windows.h>
using namespace std;
class{

[Code] ....

View 3 Replies View Related

C++ :: Reading / Writing File On Multiple Threads?

Aug 7, 2013

I'm currently working on a server for handling clients in a 2d online game and I wrote some regular fstream file code for handling the file that stores their information and I was about to implement it into the server, then I realized there might be a problem having it open multiple times concurrently, so I googled it and came up with
posts like

[URL]

I'm wondering if I can just treat it like everything else or will I have to do something specific for opening on multiple threads?

p.s. I did read those posts but I'm very new to multithreading

View 16 Replies View Related

C++ :: Reading From File And Store Values Of Each Line In Multiple Variables

May 6, 2013

So I have this text file that I am trying to read from and store the values of each line in multiple variables.

Let's say my text file contains

AXSYZ3482 Tom 100 112

and my code below here works fine when the lines in the text file is separated by spaces.

while (fscanf(fp, "%s %s %d %d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

But let's say my file was to look like this instead.

AXSYZ3482:Tom:100:112

But if i try this below...

while (fscanf(fp, "%s:%s:%d:%d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

It seem to store the entire line in userID including the ":". I want to ignore the ":"'s and store everything in between in respective varibles in the order specified above.

So first string in userID, then ignore the :, then second string in name, and ignore the next :, and so forth. How I can accomplish this?

View 2 Replies View Related

C++ :: Reading From Console Multiple Strings Of Unknown Length In Combination With Integers

Oct 10, 2014

I want to read a string of unknown length from stdin. I tried to follow the approach from this link.

[URL]....

My code is like this:

Code:

#include <iostream>
#include <string>
using namespace std;
int n;
cin >> n;
cout << "The value of n is " << n << endl;
string str;
getline(cin, str);
cout << "The entereed string is " << str << endl;

What I have noticed is that if I take integer input from cin (cin >> n in the above code before getline, the control does not stop on getline to take str as input from the console. If I don't do (cin >> n) before getline then the control stops on getline and takes the string as input.

What is the best way to read from console multiple strings of unknown length in combination with the integers?

View 1 Replies View Related

C# :: Multiple Desktop Display (Multiple Explorer Windows)

Jul 31, 2014

I want to develop an application which can host multiple views of explorer (window), where as each window is totally separate from others. So that I can have multiple desktop views through my single explorer. Any built in feature in .NET ?

View 11 Replies View Related

C/C++ :: How To Make Multiple Subroutine And Return Multiple Arrays

Aug 17, 2014

how to make subroutines and return multiple arrays.Before that, i wrote my program on matlab. could my program to be structured as following?

<header>
initial value of program;
subroutine1() {
calculating the first work;
return 2 or 3 arrays;

[code].....

View 14 Replies View Related

C :: Redirecting Multiple Pipes With Multiple Children

Mar 21, 2014

I've been working on a function that works like a pipeline of a shell but receives a directory, go over it an search for every file to send it to a filter, something like this in bash "cat dir/* | cmd_1 | cmd_2 | ... | cmd_N", The only problem i have with the code is the redirection of the pipe descriptors.

Code:

int main(int argc, char* argv[]){
char** cmd;
int Number_cmd;
cmd = &(argv[2]); /*list of cmds*/
Number_cmd = argc-2; /*number of cmds*/
}

[code]....

The code is seems to work fine except when i run it with more than one command in example ("./filter DIR wc rev") in this case it returns

wc: standard input: Bad file descriptor
wc: -: Bad file descriptor
0 0 0

View 2 Replies View Related

C++ ::  Multiple Files Causes (Multiple Definition) Error?

Jul 15, 2013

I'm using multiple C++ files in one project for the first time. Both have need to include a protected (#ifndef) header file. However, when I do that, I get a multiple definition error.

From what I found from research, adding the word inline before the function fixes the error. Is this the right way to do this, and why does it work? Should I make a habbit of just declaring any function that might be used in two .cpp files as inline?

View 5 Replies View Related

C++ :: Take Max Of Multiple Elements In Multiple Vectors?

Mar 1, 2012

Say I have 5 vectors of unsigned char each of size 5. I want to take the max of each index and store it in a new vector. What is the most optimal way to accomplish this?

My slow code:

Code:
vector<unsigned char> vec1;
vector<unsigned char> vec2;
vector<unsigned char> vec3;
vector<unsigned char> vec4;

[Code]....

View 3 Replies View Related

C++ :: The Max Of N Inputs That Is Divisible By 3

Nov 29, 2013

Code:
#include <stdio.h>
int main(){
int A, B;
char decision;
printf("Do you have an integer to input? [Y/N]: ");
scanf("%c",&decision);
if(decision=='Y' || decision=='y'){

[Code]....

After entering a single integer, it doesn't scan again for another integer. What's wrong?

I'm using a mac btw, if that makes a difference with Ubuntu/Linux.

View 8 Replies View Related

C :: How To Read Inputs From File

Dec 7, 2014

I have learnt recently how to use fgetc, fputc, fputs, fgets but still I am not able to figure out how to read values from a file like for example:

12 14 1928 32993932
17 98
166 109 201

These are separate integers & i want to read them and then manipulate these individual integers like going for 2*I1, 4*I2 and so on where I1 and I2 are 12 & 14.

View 2 Replies View Related

C :: 2 User Inputs For Calculation

Apr 9, 2014

I want to have calculations take place inside a switch statement that calls the appropriate function. The menu portion of the program works well, but I can't figure out how to let the user actually input 2 different numbers to the functions. My questions are:

1. If I use scanf to assign values to the variables, what determines end of input to each variable? (ie.. scanf("%d%d", &a, &b) what is the end of a, what is the end of b?)

2. Should I assign the variables to user input inside the switch, or try to do it in the functions below?

3. Is there something I haven't thought to ask that will screw me over? I'm really new to this.

Code:
#include<stdio.h>
int add(int b, int a);
int mult(int b, int a);
main() {

[Code] ....

This really was a test of multilayer menu, but I want to add functionality if I can.

Changed a variable before posting and didn't change all the conditions testing it.

View 3 Replies View Related

C :: Keep User From Putting Inputs

Sep 27, 2013

On my program I use a counter to count to 10, then i ask for a string, in this case "yes" or "no", during the count, i want to keep the user from putting inputs in, due to the fact that if they put both "yes" and "no" before the program reads the string.

Code:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
}

[code]....

View 3 Replies View Related







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