Like Tree1Likes

syntax error : 'bad suffix on number'

This is a discussion on syntax error : 'bad suffix on number' within the C++ Programming forums, part of the General Programming Boards category; Feels like it's right in front of me and i don't see it MAIN Code: #include "inventory.txt" #include "myfile.h" #include ...

  1. #16
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Feels like it's right in front of me and i don't see it
    MAIN
    Code:
    #include "inventory.txt"
    #include "myfile.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
    	struct Item myArray[Max];
    	read( myArray, "inventory.txt");
    my header
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    const int Max=100;
    struct Item
    {
    	string ID;
    	int sold;
    	int rem;
    };
    void read(struct Item Itemlist[], string filename)
    {
    
    
    
    
    	std::ifstream infile(filename);
        std::ofstream outfile(filename);
    	infile.open(filename);
    	for(int i=0; i<Max; i++)
    	{
    		infile >> Itemlist[i].ID;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].sold;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].rem;
    	}
    }
    and i havent changed that text file

    same error for every line of the text file: error C2059: syntax error : 'bad suffix on number'

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Remove this line
    Code:
    #include "inventory.txt"
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #18
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    main
    Code:
    #include "inventory.txt"
    #include "myfile.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
    	struct Item myArray[Max];
    	read( myArray, "inventory.txt");
    header
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    const int Max=100;
    struct Item
    {
    	string ID;
    	int sold;
    	int rem;
    };
    void read(struct Item Itemlist[], string filename)
    {
    
    
    
    
    	std::ifstream infile(filename);
        std::ofstream outfile(filename);
    	infile.open(filename);
    	for(int i=0; i<Max; i++)
    	{
    		infile >> Itemlist[i].ID;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].sold;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].rem;
    	}
    }

  4. #19
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The code seems to haven't change at all!

    In the main file, remove the first line.

    Also paste what the compiler says.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #20
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    sorry posted that last one twice and ok i just removed it
    Code:
    #include "myfile.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
    	struct Item myArray[Max];
    	read( myArray, "inventory.txt");
    header and text file are still the same

    now the error is no more, but i get nothing on the output screen I need it to read from that text file

  6. #21
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Why should you? You only call read function.

    And why are you using a struct instead of a Class? This would simplify things a lot..
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #22
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    let me post you more of the code so you can see

  8. #23
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    my fault thought it better not to post the full code but i should have here is main


    Code:
    #include "myfile.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
    	struct Item myArray[Max];
    	read( myArray, "inventory.txt");
    	Print( myArray, 4 );
    	Print( myArray, 8 );
    	Print( myArray, 16 );
    
    
    	selectionSort( myArray, Max );
    
    
    	for (int x=0; x < Max; x++)
    	{
    		Print( myArray, x );
    	}
    	system("pause");
    	return 0;
    }

    full header
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    const int Max=100;
    struct Item
    {
    	string ID;
    	int sold;
    	int rem;
    };
    void read(struct Item Itemlist[], string filename)
    {
    
    
    
    
    	std::ifstream infile(filename);
        std::ofstream outfile(filename);
    	infile.open(filename);
    	for(int i=0; i<Max; i++)
    	{
    		infile >> Itemlist[i].ID;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].sold;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].rem;
    	}
    }
    
    
    void Print(struct Item Itemlist[], int recNum )
    {
    	cout << Itemlist[recNum].ID << " " << Itemlist[recNum].sold << " " << Itemlist[recNum].rem <<endl;
    	return;
    }
    
    
    void selectionSort(struct Item list[], int length ) 
    {
    	//local vars
    	int smallestPos;
    	int temp;
    	// for every position in the array
    	for( int i=0; i<length; i++ )
    	{
    		// find the smallest element starting at that position
    		smallestPos = i;
    		for( int j=i+1; j<length; j++ )
    		{
    			if ( list[j].rem < list[smallestPos].rem )
    			{
    				// found a smaller one, remember it and keep going
    				smallestPos = j;
    			}
    		}
    		// see if we found something smaller than list[i]
    		if( list[i].rem > list[smallestPos].rem )
    		{
    			// we did find a smaller one, so swap with list[i]
    			temp = list[i].rem;
    			list[i] = list[smallestPos]; 
    			list[smallestPos].rem = temp; 
    		}
    	}
    	// done sorting the array
    }


    im not sure why i switched from a class to a struct think i just got nervous when i couldnt find my error

  9. #24
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    So here is an updated code

    here's MAIN
    Code:
    #include "inventory.txt"
    #include "myfile.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    int main()
    {
    	std::ifstream infile("inventory.txt");
        std::ofstream outfile("inventory.txt");
    	infile.open("inventory.txt");
    	outfile.open("inventory.txt");
    	
    	Item myArray[Max];
    	read( myArray, "inventory.txt");
    	Print( myArray, 4 );
    	Print( myArray, 8 );
    	Print( myArray, 16 );
    
    
    	selectionSort( myArray, Max );
    
    
    	for (int x=0; x < Max; x++)
    	{
    		Print( myArray, x );
    	}
    	system("pause");
    	return 0;
    }
    here's my header
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    const int Max=100;
    class Item
    {
    public:
    	string ID;
    	int sold;
    	int rem;
    };
    void read(Item Itemlist[], string filename)
    {
    
    
    
    
    	std::ifstream infile(filename);
        std::ofstream outfile(filename);
    	infile.open(filename);
    	outfile.open(filename);
    	for(int i=0; i<Max; i++)
    	{
    		infile >> Itemlist[i].ID;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].sold;
    		cin.ignore(100, ' ');
    		infile >> Itemlist[i].rem;
    	}
    	infile.close();
    	outfile.close();
    
    
    }
    
    
    void Print(Item Itemlist[], int recNum )
    {
    	cout << Itemlist[recNum].ID << " " << Itemlist[recNum].sold << " " << Itemlist[recNum].rem <<endl;
    	return;
    }
    
    
    void selectionSort(Item list[], int length ) 
    {
    	//local vars
    	int smallestPos;
    	int temp;
    	// for every position in the array
    	for( int i=0; i<length; i++ )
    	{
    		// find the smallest element starting at that position
    		smallestPos = i;
    		for( int j=i+1; j<length; j++ )
    		{
    			if ( list[j].rem < list[smallestPos].rem )
    			{
    				// found a smaller one, remember it and keep going
    				smallestPos = j;
    			}
    		}
    		// see if we found something smaller than list[i]
    		if( list[i].rem > list[smallestPos].rem )
    		{
    			// we did find a smaller one, so swap with list[i]
    			temp = list[i].rem;
    			list[i] = list[smallestPos]; 
    			list[smallestPos].rem = temp; 
    		}
    	}
    	// done sorting the array
    }


    Text file looks like this
    Code:
    619847GBE 641 998
    418712IMB 107 867

    Just don't know what i'm doing wrong
    error C2059: syntax error : 'bad suffix on number' And i get this for every line in my text file

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why on earth are you insisting on including your data file as code? Whatever gave you the idea that this is the right thing to do? What gave you the idea that, after having been told thrice to remove it, that you should bring it back?
    You never, ever #include anything except code. It is never the right thing to do.

    As a side note, using an elaborated name (struct Item list[] instead of just Item list[]) is only necessary in C and very unidiomatic for C++.
    Elysia likes this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #26
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    I kept putting it back as a desperate attempt hoping it would read through the file, but no. I've yet to find the mistake and im using classes

  12. #27
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    OK, here are some problems you have.
    In your main, you have this code:
    Code:
        std::ifstream infile("inventory.txt");
        std::ofstream outfile("inventory.txt");
        infile.open("inventory.txt");
        outfile.open("inventory.txt");
    The first of these lines opens the file "inventory.txt" for reading.
    The second opens it for writing and truncates it, unless this fails because the file is already open for reading (will fail on Windows, but succeed on Linux).
    The third closes the file opened for reading and opens it again. If opening for writing in the line above succeeded, this is the point you have finally lost the contents.
    The fourth closes the file opened for writing (if that succeeded) and opens it again for writing, making another attempt at truncating it. The same thing will happen as the first time.

    Then, without closing the file, you call read, where you have exactly the same code again, with exactly the same problems.

    And all of that is under the assumption that it could even find the file in the first place (hint:it couldn't, which is why your program doesn't work). Why? Because you use an unqualified file name to look for the file, which means it will look in the current working directory. The cwd is, if you run the thing from Visual Studio, the root directory of your project, not your desktop (where, according to the include in your original program, your data file sits). If you run the program by double-clicking on the exe, it will be the directory the exe is in, not your desktop.

    The thing about programming, especially in C++, is that trial and error leads you nowhere. Unless you analyze the error and understand why it occurred, nothing you do will get you closer to a solution. On the contrary, it is likely to lead you away. Compare your original program, where you only had two redundant open attempts of your file (at least one of which would kill your program's functionality) to the final version, where there are seven.
    Never add a line of code without understanding it, hoping it will help. It won't. Only ever add lines to your program that you actually understand.

    To fix your problems:
    1) Remove the include.
    2) Remove all lines that use ifstream or ofstream in main.
    3) Remove all lines that use ofstream in read.
    4) Remove the infile.open line in read.
    5) Either copy the data file to a place where the program can find it (your project root if you use Visual Studio), or use an absolute path in the program to refer to it. (Remember that you have to escape backslashes in string literals.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #28
    Registered User
    Join Date
    Dec 2012
    Posts
    23
    Found my mistake! Post to come

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    Quote Originally Posted by CornedBee View Post
    ...(Remember that you have to escape backslashes in string literals.)
    Or use raw string literals: C++11 - Wikipedia, the free encyclopedia
    Wonderful addition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    FWIW, there was no reason to use class over struct here. The only difference is that it then defaults to private instead of public, but since you just go and make it all public again you may as well have just been using struct.

    You're also misusing headers. They aren't really meant to contain most of your code, they should most often only contain prototypes, inline functions, and template definitions.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xorq: Error: suffix or operands invalid for `xor'
    By Tsus86 in forum C Programming
    Replies: 2
    Last Post: 09-21-2011, 02:49 AM
  2. Replies: 0
    Last Post: 10-13-2010, 04:36 AM
  3. Overloaded Function/Bad Number Suffix
    By zerkz in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2009, 12:37 PM
  4. Strange struct syntax; type name:number
    By OnionKnight in forum C Programming
    Replies: 1
    Last Post: 11-19-2006, 07:23 PM
  5. Replies: 2
    Last Post: 09-12-2006, 04:50 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21