473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getline and C

6 New Member
Hello,

I am new at computer programming and I am having trouble using getline() in C.
I use an input file that contains 12 variables on every line.
From the printf statements below, I notice that scanf, although risky, does the job correctly.

However, getline skips the first line from the input file and evaluates the last line twice.
Do you have any idea what I am doing wrong?

Thank you for your comments!
Regards,
Tom

#include <stdio.h>

float aa,ab,ac,ba,bb, bc,ca,cb,cc,da, db,dc;
struct point {
float x;
float y;
float z;
};
char *memal;
int nbytes = 1000;

main()
{

while (getline(&memal , &nbytes, stdin) != -1)
{
scanf("%6f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f", &ca, &cb, &cc, &aa, &ab, &ac, &da, &db, &dc, &ba, &bb, &bc);

struct point ptx0 = {aa, ab, ac};
struct point ptx1 = {ba, bb, bc};
struct point ptx2 = {ca, cb, cc};
struct point ptx3 = {da, db, dc};

printf("%f\n", ptx2.x);
printf("%f\n", ptx2.y);
printf("%f\n", ptx2.z);
printf("%f\n", ptx1.z);

etc.
Mar 21 '07 #1
2 2370
horace1
1,510 Recognized Expert Top Contributor
the getline() will skip every other line. If all lines contain 12 values just check the conversion works OK, e.g.
Expand|Select|Wrap|Line Numbers
  1. while (scanf("%6f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f %7f", 
  2.     &ca, &cb, &cc, &aa, &ab, &ac, &da, &db, &dc, &ba, &bb, &bc)==12)
  3. {
  4.   // 12 values read ..
  5. ...
  6. }
  7. // read failed
  8.  
Mar 21 '07 #2
raindeer
6 New Member
Thank you for your comment!

I did what you indicated, replacing the getline function using scanf as a test.
Now it sometimes skips lines in the middle of the input file as well, even though they all contain 12 variables. However, the last line is only evaluated once.
The input file is actually the result of an other program that puts these 12 variables on a line, followed in the end by \n, so I guess there should be no difference between these lines.

Here's part of the program where the input file is being created.

for (i=0; i< imax; i++)
{
if (num == (2+i*7)){
struct bead bdeth = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f ", bdeth.x, bdeth.y, bdeth.z);
}
else if (num == (3+i*7)){
struct bead bdori = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f ", bdori.x, bdori.y, bdori.z);
}
else if (num == (4+i*7)){
struct bead bdpro = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f ", bdpro.x, bdpro.y, bdpro.z);
}
else if (num == (7+i*7)){
struct bead bdmet = {xx, yy, zz};
printf("%6.3f %6.3f %6.3f\n", bdmet.x, bdmet.y, bdmet.z);
}
Mar 21 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3549
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline function at first and then I use the cin>> function then the next time cin.getline function does not work.
5
7746
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program that shows how to use the 'getline' function. It said: "The getline function allows you to specify how many bytes you will get from the users input. Each character the user's types takes up one byte. So if, in the getline function, you specify...
1
2141
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I could state getline's first parameter supports "FROM". The second parameter supports "TO". // later
10
5605
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'const char *' to 'char *' Conversion loses qualifiers I have a data file called Standard.udf The Data File (not the problem): The data in the file is in text format, and was created using a function in this program with...
14
3876
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy enough, but I can't get the getline(file_name array_name) to work. So...I am thinking it is definitely...
18
8245
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include <iostream> #include <fstream> #include <cstdlib> #include <string>
2
3450
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip char* cstr; std::cout << "House/Appartment # "; std::cin.getline(cstr, CHAR_MAX); //<<==seg fault
5
3637
by: allspamgoeshere3 | last post by:
Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating. Inside the function that reads the content of the file there is a loop that basically looks like this ifstream file("levels\\level1.txt"); string line; getline(file, line);
33
25205
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
3
25204
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a vector array of strings, but i cant get stringstream working :(
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.