C++ :: Getline Skipping First Line?
Aug 21, 2014text file is
hello
there
void readFile(char[], ifstream&);
address records[numbersAdd];
int main(){
char xml[30];
cout<<"Enter file name ";
[code].....
text file is
hello
there
void readFile(char[], ifstream&);
address records[numbersAdd];
int main(){
char xml[30];
cout<<"Enter file name ";
[code].....
why does this happen that if i use
string ans;
for(int i=1; i<3; i++)
{
cout << "enter an option" << endl;
getline(cin,ans)
}
in a loop it always skips the first time but in the next times it asks?
I used getline to import EMPLOYEE's First and Last Name from a txt file. After calculating the weights ans stuff. Now when i try to write to output txt file am having the below issue in which its not putting it in one line.
***CODE***
fout << "Note: This report for " << employee << " was prepared according to the fair practice of the University." << endl ;
***THIS IS WHAT ITS PRINTING***
Note: This report for FIRSTNAME LASTNAME was prepared according to the fair practice of the University.
I also tried like this;
***CODE***
fout << "Note: This report for " << getline(fin, employee) <<" was prepared according to the fair practice of the University." << endl ;
***THIS IS WHAT ITS PRINTING***
Note: This report for 0 was prepared according to the fair practice of the University.
this second code puts everything in one line but its showing 0 (zero) instead of the employee's first and last name.
I've been stuck on this for hours, and have scoured the internet for any insight on this particular issue with no luck.
I'll simplify the problem here. I have a lines in an input file:
John Smith 1234 Main St New York
Carol Thomas 5678 5th Ave Columbus
...
and I am reading into character arrays using code like this:
infile.getline(name,20);
infile.getline(street,15);
infile.getline(city,10);
but when I print the output using something like this:
outfile << "Owner Name: " << name << endl;
outfile << "Address: " << street << city << endl;
(let's pretend I included spaces between the address components, which I omitted to save space)
I get an output file of:
Owner Name: John Smith
Address:
The output stops after the name variable, which I believe is stored correctly. I don't think anything is storing in the address pieces, this is the problem.
I think it's because I'm not using getline() properly in this case. Can you string together multiple getline()s like that?
If this was my design, I'd read in strings instead, but that is not possible in this case.
Taken from Accelerated C++ book, I modified those code to use getline() instead of std::cin >> only to find out that the output has extra line. Why is that so?
#include <iostream>
#include <string>
int main() {
std::cout << "What is your name?" << std::endl;
std::string name;
[Code] .....
What is your name?
Naruto
*****************
* *
* Hello, Naruto
*
* *
*****************
Notice one asterisk after the greeting where it should be in the same line as the greeting.
I have written a C++ program in which a user need to input a character. My problem is it's working fine for the first time but as it goes through the code second time, it skips the cin statement and takes the previous defined value. how can i make cin statement work for the second time also?
View 5 Replies View Relatedim a newbie C user and im having a little trouble in these for loop of mine im using. the first iteration is all fine but on the second and succesive iterations the first gets statement is skipped. im making a program that would ask the user to input multiple informations for atleast 5 people. i was also asked to use structures.. here is the code i have come up so far.. ive been stuck in it for like 3 hours now.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
}
[code]....
gets part for the line "Enter ID Number is skipped on the second iteration..
When I call the member function in the main function, two functions are working fine but the third one(print()) is not. The program stops after executing the read_ages function and nothing printed on the screen. This is really strange and I could not find any problem after spending hours and had to post it here.It is a very simple program but I cant find the bug. It is a multiple file program and I am using MinGW as a compiler.
//File 1
#include<vector>
#include<string>
class Name_pairs {
private:
std::vector<std::string>names;
std::vector<double> ages;
[Code] ...
//I haven't made the sort function yet since I am stuck with the print(). Seems //like the compiler is skipping the print() function.
the problem that I'm running into is that the compiler skips to the end when I put my initials in //User input > "Initials"
I'm using Microsoft Visual C++ 2010 Express.
Here is the assignment:
Plan and code a program to do the following. You found an exciting summer job for 5 weeks. It pays $15.50 per hour. You will input the number of hours per week that you worked and then compute your total earnings. You must pay taxes of 14%. After paying taxes, you will spend 20% of your money on cloths and 5% on school supplies. After buying clothes and supplies, you will use 25% of the remaining money for savings.
Input
Your 3 initials and the hours for each of the 5 weeks. Use the following numbers of hours for your first test 25, 30, 20, 23, 22.
Calculations
Gross pay is the rate of pay times the sum of all hours you worked. Use CONSTANTS for each of the following rates:
Tax rate 14% of the gross earnings
Clothing 20% of earnings after taxes
School supplies 5% of earnings after taxes
Savings 25% of earnings after taxes and expenses
Output:
Output your initials, total hours worked, gross earnings, taxes, net earnings after taxes, clothing expense, supplies expense, amount going to savings and amount left to spend. Output must be aligned to the right as shown with 2 decimals in all numbers. Sample output:
Initials ABC
Total Hours Worked 120.00
Gross Earnings 1860
Taxes paid 260.40
Net Earnings 1599.60
[code]....
Turn in:Be sure your output file contains user prompts and what was entered by the user. In addition to the results of your program processing. Run with above listed data.
Here is my code
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double Initials, TotalHours, GrossEarn, TaxesPaid;
[code]....
I am supposed to write a program that reads lines from files and see if the first character is an odd number, if so it will out put the remaining characters to output file, else itll seek to the beginning of the next line, this is what i came up with but its not working as it should, im not sure if its the version im using or the program I wrote, the commented part below was another algorithm I was trying to use.
#include<fstream>
#include<iostream>
using namespace std;
int main() {
char c;
int i =0;
[Code] ....
My text file was as follows:
input.txt
3Ali
4Sami
6Rashid
3Aya
output was unexpected , although some was relatively write but not as I wanted...
I am trying to put pieces of a csv file into an array, but i only want to put certain pieces of it. This is the information that i have
Player,Current Team, Age , Nat , Position ,From,To,Transfer fee
Gareth Bale, Real Madrid,24,Wales,RW,Tottenham,Real Madrid CF,91000000
Edinson Cavani,PSG,26,Uruguay,CF,SSC Napoli,PSG,64500000
Falcao, Monaco,27,Columbia,CF,Atlético Madrid,Monaco,60000000
Neymar, FC Barcelona,21,Brasil,LW,Santos,FC Barcelona,57100000
Mesut Özil, Arsenal,24,Germany,AM,Real Madrid ,Arsenal,50000000
James Rodríguez,Monaco, 21,Columbia,RW,FC Porto,Monaco,45000000
With this i want to skip the name and team but need age.
I am attempting to write a program that converts binary to base10, and vice versa.
But in the function for converting Base10 to Binary, just as it reaches the line of code
int* binary = new int [a];
it skips straight to the int main()
All I'm attempting to do with that line of code is initialize the variable "a" into the elements of the array "binary".
[URL] ....
How can a mulitline string be read line by line
ex: str = "PERIOD="week"
DAY="day"
TIME="time"";
Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.
The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.
The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.
The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)
The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.
The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.
The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.
The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:
If a patron buys a ticket at the door, a command will be on a line by itself:
BUY TICKET k
where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.
Code:
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000
[Code].....
I've just started learning from "Jumping into C++". Great book. Unfortunately, I've also encountered my first snag. Practice problem number 3 from chapter 4 tells me to make a small calculator that takes one of the four arithmetic operations and its two arguments as input and give the result as output. Here's what my newbie mind came up with:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
[Code] .....
The thing that I don't get is why doesn't the function "getline" work. It's worked in previous programs. The program seems to work if I simply replace getline with a simple "cin". I could easily use that as a cheap fix but I am interested in knowing why "getline" refuses to work anymore....
I'm currently trying to write a while loop that checks if the text file has read all the contents inside. I've tried using
while(!in.eof())
but as usual it executes my loop an extra iteration, printing my last output twice. I am reading my data in from a method inside a class, so I cannot use getline as my while test to check if the file has read input or not. Is there any way to force my loop to check if the end of file has been read before the eof() test is executed?
I am just trying to get a code going for a mock test and to get use to the getline and IF operations, but it seems I have ran into an issue[URL] is a link to the code I have written, and I can use getline to give a value to my variable, but it seems like it gets lost once I try to use the IF function. Am I not using the right variable type?
View 14 Replies View RelatedI'm using getline to read in from a file line by line but it skips the first two whitespaces.
Example input:
1 0.7 mountain and grant
How it's being stored:
10.7mountain and grant
I have just started working through "Jumping into C++". I am at the section on appending strings. The tutorial mentions the getline function but I can not seem to get it to activate. There is no mention of any other inclusions.
Code:
#include <iostream>
#include <string>
using namespace std;
[Code] ....
I note that the getline function color remains black while other functions are green. I presume this means that Codeblocks has not associated it with any of the listed header files. Has the tutorial omitted this detail?
How the getline function works.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
[Code] ......
getline(&inputstr, 128, NULL); getline gets a line in a data file.
I assume that inputstr[128] is the name of the file? why is the number of the array in the getline function....
I am trying to use getline as the conditional for a while loop as seen below.
string filename, guard_name;
int call_number;
vector <Seniority> SeniorityList;
ifstream SeniorityFile;
[Code] ....
However, when I compile the code, I get the following error message on the line with the while loop.
error C2780: 'std::basic_istream<_Elem,_Traits>
&std::getline(std::basic_istream<_Elem,_Traits>
&,std::basic_string<_Elem,_Traits,_Alloc>
&)' : expects 2 arguments - 3 provided
The program will be reading in a .csv file, so I need my program to be able to deal with values separated by commas. Is there a reason why it will not accept that conditional statement?
i got a file (teams.csv) which has the following content:
id,name
501,Abilene Chr
502,Air Force
503,Akron
504,Alabama
Now i want to read line by line using getline(). How will i do that?
I have a problem with the switch statement because it doesn't prompt me to input something.
#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;
//Compiler: Dev C++ and cygwin
void rd() {
string txt;
[Code] ....
I want to get rid of this kind of output ...
I am currently having trouble to have getline to read line from the file. Error is: "no instance of overloaded function "getline" matches the argument list"
code is as follows:
std::ifstream config("config.txt");
string process[4];
int linecount = 1;
if (config.is_open)
{
while (config.peek() !=EOF)
{
getline(config, process);
linecount++;
}
}
Down on line 74, it does not wait cor an input, it simply says ...you failed.
#include <iostream>
#include <string>
#include <Windows.h>
#include <cstdlib>
using namespace std;
//prototyping--
void flater();
[Code] ....
I need to write a ANSI program to print out each command line argument on a separate line using a for-loop. also it need to print the name of the executable .so far I have
Code:
#include <stdio.h>
int main(int argc, char **argv) {
int i;
printf("")
[code]....