469,616 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,616 developers. It's quick & easy.

error C2001: newline in constant-Help!

1
Trying to create a program which reads and echos the contents of an input data file, which consists of two records. When I complile my code I keep getting a "error C2001: newline in constant" error. Read every forum regarding the "error C2001: newline in constant" error but I am still not clear as to where my code is erroring out. Can anyone help point me in the right direction?
Thanks

My code:

Expand|Select|Wrap|Line Numbers
  1. //************************************************************************************************
  2. // Test Program: Chapter 6            A noninteractive program which reads and echos input data file.
  3. //            
  4. //
  5. //************************************************************************************************
  6. #include<iostream.h>
  7. #include<iomanip>
  8. #include<fstream>                    //For input data file.
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14.     float side;                        // 1st input data field on each record: must be >=0
  15.     float area;                        // 2nd input data filed on each record: must be >=0
  16.     ifstream inFile;                // Data file: each record shows side 
  17.  
  18.  
  19.     //Attempt to open the input data file
  20.     inFile.open("sidearea.dat");
  21.     if(!inFile)                    // If file can't be found (we're in a fail state):
  22.     {
  23.         cout<<("Unable to open input file, program abnormally ended\n");
  24.         return 1;                    // Terminate program early, with return code=1:
  25.     }
  26.  
  27.     //Get first pair of input data values for side and area, then echo print:
  28.     inFile>>side>>area;
  29.     cout<<"Record1:Side is"<<side<<"and area is"<<area<<endl;
  30.  
  31.     //Check 1st record for I/O errors, then for valid data values:
  32.     ;if(!inFile)
  33.     {
  34.         cout<<"I/O Error on input file, program abnormanlly ended\n";
  35.         return 1;
  36.     }
  37.     else
  38.     {
  39.         if(side<0)
  40.             cout<<"Warning side is invalid, must be = or > 0"<<endl;
  41.         if(area=(side*side))
  42.             cout<<"Warning area is invalid, must not be = to side square"<<endl;
  43.     }
  44.  
  45.     //Get second pair of input data values for side and area
  46.     inFile>>side>>area;
  47.     cout<<"Record2:Side is:<<side<<and area is<<area<<endl;
  48.  
  49.     //Check 2nd record for I/O errors, then for valid data values:
  50.     ;if(inFile)
  51.     {
  52.         cout<<("I/O Error on input file, program abnormally ended");
  53.         return 1;
  54.     }
  55.     else
  56.     {
  57.         if(side<0)
  58.             cout<<"Warning side is invalid, must be = or > 0"<<endl;
  59.         if(area=(side*side))
  60.             cout<<"I/O Error on input file, program abnormally ended"<<endl;
  61.             }
  62.  
  63.     //Terminate program normally:
  64.     cout<<"No I/O Errors in input file, pgm. terminated normally"<<endl;
  65.     return 0;
  66. }
Feb 20 '07 #1
3 14246
sicarie
4,677 Expert Mod 4TB
Trying to create a program which reads and echos the contents of an input data file, which consists of two records. When I complile my code I keep getting a "error C2001: newline in constant" error. Read every forum regarding the "error C2001: newline in constant" error but I am still not clear as to where my code is erroring out. Can anyone help point me in the right direction?
Thanks

My code:

/************************************************** **********************************************
// Test Program: Chapter 6 A noninteractive program which reads and echos input data file.
//
//
//************************************************** **********************************************
#include<iostream.h>
#include<iomanip>
#include<fstream> //For input data file.
using namespace std;


int main()
{
float side; // 1st input data field on each record: must be >=0
float area; // 2nd input data filed on each record: must be >=0
ifstream inFile; // Data file: each record shows side


//Attempt to open the input data file
inFile.open("sidearea.dat");
if(!inFile) // If file can't be found (we're in a fail state):
{
cout<<("Unable to open input file, program abnormally ended\n");
return 1; // Terminate program early, with return code=1:
}

//Get first pair of input data values for side and area, then echo print:
inFile>>side>>area;
cout<<"Record1:Side is"<<side<<"and area is"<<area<<endl;

//Check 1st record for I/O errors, then for valid data values:
;if(!inFile)
{
cout<<"I/O Error on input file, program abnormanlly ended\n";
return 1;
}
else
{
if(side<0)
cout<<"Warning side is invalid, must be = or > 0"<<endl;
if(area=(side*side))
cout<<"Warning area is invalid, must not be = to side square"<<endl;
}

//Get second pair of input data values for side and area
inFile>>side>>area;
cout<<"Record2:Side is:<<side<<and area is<<area<<endl;

//Check 2nd record for I/O errors, then for valid data values:
;if(inFile)
{
cout<<("I/O Error on input file, program abnormally ended");
return 1;
}
else
{
if(side<0)
cout<<"Warning side is invalid, must be = or > 0"<<endl;
if(area=(side*side))
cout<<"I/O Error on input file, program abnormally ended"<<endl;
}

//Terminate program normally:
cout<<"No I/O Errors in input file, pgm. terminated normally"<<endl;
return 0;
}
If this was copied directly, I would check this line:
//Check 1st record for I/O errors, then for valid data values:
;if(!inFile)
the 'if' statement has a ';' in front of it - that shouldn't be there.
Feb 20 '07 #2
Banfa
9,064 Expert Mod 8TB
What sicarie says is a minor error that is probably not stopping program compilation.

The "New line in constant" error is nearly always the result of failing to put the terminating " on a string constant.

In our case it happens on the cout line
Expand|Select|Wrap|Line Numbers
  1. //Get second pair of input data values for side and area
  2. inFile>>side>>area;
  3. cout<<"Record2:Side is:<<side<<and area is<<area<<endl;
  4.  
where you appear to have missed out several "
Feb 20 '07 #3
sicarie
4,677 Expert Mod 4TB
What sicarie says is a minor error that is probably not stopping program compilation.

The "New line in constant" error is nearly always the result of failing to put the terminating " on a string constant.

In our case it happens on the cout line
Expand|Select|Wrap|Line Numbers
  1. //Get second pair of input data values for side and area
  2. inFile>>side>>area;
  3. cout<<"Record2:Side is:<<side<<and area is<<area<<endl;
  4.  
where you appear to have missed out several "
Oh dang. I was thinking that couldn't just be it, but I missed that several times. Thanks, Banfa!
Feb 20 '07 #4

Post your reply

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

Similar topics

3 posts views Thread by Jacob Bensabat | last post: by
6 posts views Thread by Leo R | last post: by
13 posts views Thread by albert_reade | last post: by
3 posts views Thread by Sajeena | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.