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:
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")
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?
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?
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
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?
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?
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...
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.
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 .
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.
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.
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
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?
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?
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 ?
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
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?
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?
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.
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.
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.
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.