Help with a build error in Visual Studio

In summary, this is a very simple program about Class with Inventory.h, Inventory.cpp and source.cpp. I reduced it to very simple just displaying two message and it just won't build. It gave me error message I don't follow even after reading the error code online. I follow the example and syntax from the book. I am running out of idea what to do.
  • #1
yungman
5,718
241
TL;DR Summary
I get Building error that I looked online following the error code and compare the code to the book, I cannot figure what I did wrong.
This is a very simple program about Class with Inventory.h, Inventory.cpp and source.cpp. I reduced to very simple just displaying two message and it just won't build. It gave me error message I don't follow even after reading the error code online. I follow the example and syntax from the book. I am running out of idea what to do.

This is the Inventory.h:
C++:
#ifndef Inventory_H
#define Inventory_H

class Inventory
{
public:
    void writeRec();
    void displayRec();
}
#endif

Here is the Inventory.cpp:
C++:
#include <iostream>
#include "Inventory.h"

void Inventory::writeRec()  
 {    std::cout << " In writeRec.\n\n";}
void Inventory::displayRec()
 {    std::cout << " In DisplayRec.\n\n";}

Here is the source.cpp:
C++:
#include <iostream>
#include "Inventory.h"

int main()
{
    Inventory Inv;
    Inv.writeRec();
    Inv.displayRec();
    return 0;
}
Can anyone see what I did wrong? Here is the error list:
Error list.jpg


thanks
 
Technology news on Phys.org
  • #2
  • #3
The error messages in C++ are sometimes not very helpful, but at least the error message asks "(did you forget a ';'?)". That's the clue.
 
  • Like
Likes Vanadium 50
  • #4
Thanks for the reply.

It is not the ';' that's the problem. Like line 4 of source.cpp is int main(). It cannot have ';' after that. Same as line 4 of Inventory.cpp, it's
void Inventory::writeRec() { std::cout << " In writeRec.\n\n";}

It cannot have ';' after void Inventory::writeRec().

Something else is going on. Yes, I spent a few hours looking on line before I post.

Thanks
 
  • #5
yungman said:
Thanks for the reply.

It is not the ';' that's the problem. Like line 4 of source.cpp is int main(). It cannot have ';' after that. Same as line 4 of Inventory.cpp, it's
void Inventory::writeRec() { std::cout << " In writeRec.\n\n";}

It cannot have ';' after void Inventory::writeRec().

Something else is going on. Yes, I spent a few hours looking on line before I post.

Thanks
wdfd.png
 
  • Like
Likes Timo and yungman
  • #6
Jarvis323 said:
That's the part it actually show a red wiggle as shown below:
Inventory.h error.jpg


The message is "expect a declaration". But if you look at other example of .h files, mine is just typical format. I checked over like 10 times to make sure there is no syntax error before I posted.

BTW, I did not thank you enough on the linking external files, that really helps. The thread was closed before I can express this. That was EXACT what I kept asking. There got to have a way to mix ADD-->New on some files and just ADD--> Existing to link some files that was created before and compile together. That was BIG(HUGE) for me. Thanks.
 
  • #7
You are missing a ';' in the green box I made;

Note that include just does a copy and paste. So this is what the compiler is trying to compile.
C:
#include <iostream>
class Inventory
{
public:
    void writeRec();
    void displayRec();
}  // forgot ';' here
int main() // error is here, int can't follow Inventory, (did you forget a ';'?)
{
    Inventory Inv;
    Inv.writeRec();
    Inv.displayRec();
    return 0;
}

To be more specific, you can declare an instance of the class at the end like this: class A{ } aInstance; Or you can just define the class: class A{ }; If you forgot a ';' at the end, the compiler will keep reading on expecting a declaration to be coming, or a ';'. Since you forgot a ';', the compiler sees "int" which is an illegal variable name, or illegal to follow the class before a ';' in any case.
 
Last edited:
  • Like
Likes FactChecker and yungman
  • #8
Jarvis323 said:
You are missing a ';' in the green box I made;

Note that include just does a copy and paste. So this is what the compiler is trying to compile.
C:
#include <iostream>
class Inventory
{
public:
    void writeRec();
    void displayRec();
}  // forgot ';' here
int main() // error is here, int can't follow Inventory, (did you forget a ';'?)
{
    Inventory Inv;
    Inv.writeRec();
    Inv.displayRec();
    return 0;
}

To be more specific, you can declare an instance of the class at the end like this: class A{ } aInstance; Or you can just define the class: class A{ }; If you forgot a ';' at the end, the compiler will keep reading on expecting a declaration to be coming, or a ';'. Since you forgot a ';', the compiler thinks "int" is the name you tried to use for an instance of the class, since that is what follows the class in source.cpp. It tells you that int can't follow the class, ';' needs to follow it, or a valid name then a ';'.
OH my god.! You saved my day!

Thanks a million!
 
  • #9
DaveC426913 said:
When I look to Stack Overflow (which is where you should definitely be spending time) I see similar code snippets, but they have a using keyword in there
Not applicable. The code shown is not using any standard template library stuff, so using isn't needed.
 
  • #10
I think cl/VS is behind the competition in terms of generating good error messages.

With g++ I get this:
Code:
In file included from Inventory.cpp:2:
Inventory.h:9:2: error: expected ‘;’ after class definition
    9 | }
      |  ^
      |  ;
In file included from source.cpp:2:
Inventory.h:9:2: error: expected ‘;’ after class definition
    9 | }
      |  ^
      |  ;
 
  • Like
Likes yungman
  • #11
@yungman , I want to comment that your question here shows a lot of improved coding maturity and effort. Beginning programmers are often frustratingly incomplete in their posts. In my opinion, your posts are really improving rapidly and are starting to look professional. Keep up the good work!
 
  • Like
Likes Jarvis323 and yungman
  • #12
FactChecker said:
@yungman , I want to comment that your question here shows a lot of improved coding maturity and effort. Beginning programmers are often frustratingly incomplete in their posts. In my opinion, your posts are really improving rapidly and are starting to look professional. Keep up the good work!
Thank you so much.
 
  • Like
Likes Jarvis323
  • #13
Jarvis323 said:
I think cl/VS is behind the competition in terms of generating good error messages.

With g++ I get this:
Code:
In file included from Inventory.cpp:2:
Inventory.h:9:2: error: expected ‘;’ after class definition
    9 | }
      |  ^
      |  ;
In file included from source.cpp:2:
Inventory.h:9:2: error: expected ‘;’ after class definition
    9 | }
      |  ^
      |  ;
Hi Jarvis

I want to use this chance to thank you for the post in the last thread that was closed. Your advice on linking the external file is EXACTLY what I was asking for in that post. I know I can easily ADD-->New and copy the code and everything will work. My point was to find a way so I can link external Class Object to the project without having to copy and paste every time to make it work. That is I want to be able to ADD-->New for my source file and ADD-->Existing on the .h and .cpp from another location. With your instruction, that works.

I was running into other problems, by the time I can double verified with another project, the thread was closed.

This time, some how you mentioned about the Inventory.h, then I remember and screen copy the little red wiggle line. Never once the error message talked about the Inventory.h file, all the stuffs they flagged were on the .cpp! Somehow, you have the nose to smell it out, like last time. You knew exactly what I was after and got to the point!.

Don't laugh, In my experience in my career, The nose is where in make the day! I lot of time in electronics, when everything looked so right to me, but my nose was NOT happy, a lot of times, my nose was right. I am not joking, when I finished my design and did the pcb layout, I sit there and feel my guts. I feel how my chest is! If I feel tight, nervous, I keep looking. A lot of time, I would find mistake. Then all of a sudden, I feel relax. I let it go and send to the board house. I have the nose for that, just don't have the nose for programming!

This program started out a much bigger program, I looked at the message, starting to delete lines that didn't matter and then monitor the error messages and trimmed the program down to these few lines. I followed the error messages on everyone and read. NOTHING make sense. When I had more stuffs, it complained the scope resolution operator ::, complained about the "using namespace std", complained about everything but the Inventory.h file. I got rid of those codes down to the bare bone before I posted! This is really something.

thanksEDIT: In the pass, I put effort on C++, learning the names and all. I took VS for granted, all I did was one single file, just learn the few steps and never really gave me problem, particular learning the debugger, it was smooth sail.....until I have to deal with multiple files. WOW, it's NOT that simple. I have to now have a binder to print out all the stuffs I learn and write notes just like on the C++. Still have a long way to go.
 
Last edited:
  • #14
yungman said:
This time, some how you mentioned about the Inventory.h, then I remember and screen copy the little red wiggle line. Never once the error message talked about the Inventory.h file, all the stuffs they flagged were on the .cpp! Somehow, you have the nose to smell it out, like last time. You knew exactly what I was after and got to the point!.
The compiler didn't complain about Inventory.h because the contents of this header file were merged into Source.cpp before the compiler saw it. The effect of a preprocessor directive such as #include "Inventory.h" is that all of the text in that header file gets merged into the file that contains the include directive.

The first two errors you showed in post #1 were due to the compiler seeing something like this in Source.cpp:
C++:
class Inventory
{
public:
    void writeRec();
    void displayRec();
} int main()
{
    Inventory Inv;
    Inv.writeRec();
    Inv.displayRec();
    return 0;
}
As @Jarvis323 already noted, the problem was a missing semicolon after the class declaration.
 
  • Like
Likes yungman
  • #15
Thanks Mark, I did not know that's how it looks when the files are merged. So the whole program looks like this after merged with Inventory.cpp?
C++:
//Merged from Inventory.h
class Inventory
{
public:
    void writeRec();
    void displayRec();
//Main program
} int main()
{
    Inventory Inv;
    Inv.writeRec();
    Inv.displayRec();
    return 0;
}
//Merged from Inventory.cpp
void Inventory::writeRec()  
 {    std::cout << " In writeRec.\n\n";}
void Inventory::displayRec()
 {    std::cout << " In DisplayRec.\n\n";}

I never think of it this way.

Thanks for your help all this time. I really appreciate this. I think I need to spend more time on VS now that it's getting more complicate with multiple files. Before, it's like blind learning a few steps and getting the programs running. Now it's different.
 
  • #16
I have a question about Solution Explorer. Before I really messed up the .sln view 2 days ago, it used to look like this, AND it worked when I put the .h file in the Header Files folder, .cpp files in Source Files folder. With the external folder path typed in, it will work with both Add-->New and Add-->Existing from external folder.
Solution Explorer.jpg


But, of cause, I screwed up, I managed to delete both the Header Files and Source Files folders. I added back those folders in and name them exactly the same. BUT it doesn't seem to work the same. For one thing, if I create the .h file and put it in the Header Files folder, compiler cannot find it. I have to delete the .h from Header Files folder and put it in the Source Files folder as shown below. Then it will work.
New Solution Explorer.jpg


What is the difference? If it is different, how can I get back the original Solution Explorer with those folders? The sickening thing is VS REMEMBER all my mistakes, I screwed up in one project, that screw up reflects on ALL the other projects. Everything is screwed up. Is there any way to make it so whatever I do, it confines to the project opened at the time and won't affect the others?

Thanks
 
  • #17
Those "folders" are not actually folders in Visual Studio. They are just filters. The "Header Files" filter contain this filter
Code:
h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
Right click on Test Class > Add > New Filter. From the new project template, "Header Files" Filter should be like the one I just wrote.

Your files, header and source files, should be safe in your computer and you can search it through the explorer. For basic use you can just put all .h and .cpp files together in one folder, and then import (add existing item) them into Visual Studio if you have some files written outside Visual Studio.

I am not a fan of Visual Studio myself because it's slow and can get confusing for new users, even for just the solution explorer. To get the original folder I think you can just recreate a new project and copy all .cpp and .h files without using Visual Studio, and then import them to Visual Studio. For example I did create a test project called ConsoleApplication1 and the project layout looks like this:
Code:
D:\ConsoleApplication1\ConsoleApplication1.sln
D:\ConsoleApplication1\ConsoleApplication1 (in here I see all the .h and .cpp files, that are created when I Add New Item from Visual Studio)
D:\ConsoleApplication1\x64
...
 
  • Like
Likes yungman
  • #18
Thanks Gregory112 for your reply.
When you said project template, do you mean (2) I point to? I don't know those names.
Do you mean the Header Files (1) below is actually a filter?
New Solution Explorer.jpg
I followed your instruction, I right click to Test Class in (1) above and get this menu. I don't see New Filter option.
New Filter.jpg


Confusing is an understatement with VS, this is the 3rd day I am on VS solution explorer, not in C++. I don't follow about your creating a test project ConsoleApplication1. Do you mean you created a folder called ConsoleApplication1 somewhere in D drive in the computer. You put all your .h and .cpp files in it. Then you use Add-->Existing folder and import into VS? How do you do that as there is no option to import a whole folder?

I still have a long way to learn VS.

Thanks
 
  • #19
What I mean by project template is when you try to create new project. In there you can search for a C++ template, and I chose console application, which you should be choosing too if you just want to make some test C++ projects.

I think we are using different Visual Studio version, I am using 2019.

Yes, but what I did was I created the project from VS first, to let VS created all the necessary folders. For newcomers, a C++ project do not actually have obligatory directory structure, but VS created many different folders that some of them I do not know what for. After creating the project you can then move your source files inside the newly created project folders and add existing items, instead of adding existing folder because that may recreate redundant folders.

When creating new project, no need to check the "save solution file together with project files" (I forgot the exact wording of this option). This will make VS create a folder the name of your project, and will then create a directory structure similar to what I told you before.

Why is is complicated? Because a solution in VS can contain multiple "subprojects" in it, that is why there are a lot of folders and hierarchies.
 
  • Like
Likes yungman
  • #20
yungman said:
When you said project template, do you mean (2) I point to?
I believe what he meant was the dialog that opens when you create a new project. You fill in the fields with a name for the project. In post #18, the first image you show is after a project has been created.
yungman said:
Do you mean the Header Files (1) below is actually a filter?
Sort of. All of the headings that show up in Solution Explorer are described in the file with suffix .vcxproj.filters. For example, if the name of the project is Temp, there will be a file named Temp.vcxproj.filters. There will be another file named Temp.vcxproj. In VS 2017, which is what I'm using, the vcxproj file contains information about what files make up the project and how they will be built to produce the final product.

More information on project filters is here: https://docs.microsoft.com/en-us/cpp/build/reference/vcxproj-filters-files?view=vs-2019.

All I did was to do a search using vcxproj filters as the search key.
It would be good if you started doing more searches like this.
yungman said:
I followed your instruction, I right click to Test Class in (1) above and get this menu. I don't see New Filter option.
If you select Test Class, then right-click, you will see Add --> New Filter. Since you already have the Header Files and Source Files "folders" (they're not really directories that you would see in Windows Explorer), I can't think of any reason you would want to do this, unless you had somehow deleted these "folders".
yungman said:
Do you mean you created a folder called ConsoleApplication1 somewhere in D drive in the computer. You put all your .h and .cpp files in it. Then you use Add-->Existing folder and import into VS? How do you do that as there is no option to import a whole folder?
When you create a new project, you fill in a name for the project. The default name for this type of project is ConsoleApplication1. If you check the small box that says "Create directory for solution", VS will create a top-level directory named ConsoleApplication1, and a directory under it that is also named ConsoleApplication1. In Windows Explorer you can copy your .cpp files and .h files to that 2nd-level directory, and then Add--> Existing item using Solution Explorer.

If you already have a project names ConsoleApplication1, the name in the dialog template will be ConsoleApplication2, and so on. It's usually not a good idea to use the default names for your projects -- it's better to come up with a name that suggests what your project is going to do.
 
  • Like
Likes yungman
  • #21
gregory112 said:
What I mean by project template is when you try to create new project. In there you can search for a C++ template, and I chose console application, which you should be choosing too if you just want to make some test C++ projects.

I think we are using different Visual Studio version, I am using 2019.

Yes, but what I did was I created the project from VS first, to let VS created all the necessary folders. For newcomers, a C++ project do not actually have obligatory directory structure, but VS created many different folders that some of them I do not know what for. After creating the project you can then move your source files inside the newly created project folders and add existing items, instead of adding existing folder because that may recreate redundant folders.

When creating new project, no need to check the "save solution file together with project files" (I forgot the exact wording of this option). This will make VS create a folder the name of your project, and will then create a directory structure similar to what I told you before.

Why is is complicated? Because a solution in VS can contain multiple "subprojects" in it, that is why there are a lot of folders and hierarchies.

Thanks for the reply.

I tried creating a new project with console application. It came with ConsoleApplication1.cpp that say "Hello World". Why is it preferrable to do test program with console application? I still don't see the option to Add--> New Filter.

I am using VS 2019 the latest one as I just installed it 3 weeks ago.

It is not that I want to create new folders( filters), I accidentally deleted them. My mouse/computer is funny, when I highlighted a line to delete, sometimes it highlight the following lines while I hit the delete. It's too late when I notice that and the next line got deleted. In the code, I can hit Ctrl-Z to get them back, it DOES NOT work if I deleted the folder in the Solution Explorer view. I must have deleted the Source Files and Header Files folders(filters) by accident.

That's really not a serious problem if I can just create a new project and copy all my .h and .cpp files over. BUT the STUPID VS...I repeat...THE STUPID VS remember what I deleted. If I create a new project, the Header Files and Source Files folders are also GONE. Even when I open an old file that had the folders before, they are GONE.

How can any program can be this stupid. People make mistakes, each and every single program I use will start from fresh when you close and re-open the program again. Who design VS that do stupid things like this?

Here I am, until I wipe the VS and re-install it, I don't know how I can get it back. So I have to create the folders.

Thanks
 
  • #22
Mark44 said:
I believe what he meant was the dialog that opens when you create a new project. You fill in the fields with a name for the project. In post #18, the first image you show is after a project has been created.
Sort of. All of the headings that show up in Solution Explorer are described in the file with suffix .vcxproj.filters. For example, if the name of the project is Temp, there will be a file named Temp.vcxproj.filters. There will be another file named Temp.vcxproj. In VS 2017, which is what I'm using, the vcxproj file contains information about what files make up the project and how they will be built to produce the final product.

More information on project filters is here: https://docs.microsoft.com/en-us/cpp/build/reference/vcxproj-filters-files?view=vs-2019.

All I did was to do a search using vcxproj filters as the search key.
It would be good if you started doing more searches like this.
If you select Test Class, then right-click, you will see Add --> New Filter. Since you already have the Header Files and Source Files "folders" (they're not really directories that you would see in Windows Explorer), I can't think of any reason you would want to do this, unless you had somehow deleted these "folders".
When you create a new project, you fill in a name for the project. The default name for this type of project is ConsoleApplication1. If you check the small box that says "Create directory for solution", VS will create a top-level directory named ConsoleApplication1, and a directory under it that is also named ConsoleApplication1. In Windows Explorer you can copy your .cpp files and .h files to that 2nd-level directory, and then Add--> Existing item using Solution Explorer.

If you already have a project names ConsoleApplication1, the name in the dialog template will be ConsoleApplication2, and so on. It's usually not a good idea to use the default names for your projects -- it's better to come up with a name that suggests what your project is going to do.
Thanks Mark

I read the link you posted on vcxproj.filters. It said I can create the folder in "Root Folder of the Project". What is this. Again, it's the name.

I accidentally deleted the Source and Header Files(filter or folders) as described in the last post. I would be happy if there is a way to reverse it and get back the folders short of creating it myself. Also, is there any command that tell VS NOT to do the same thing on all my files? I accidentally deleted the folders, the Source and Header folders are ALL gone in ALL my projects. It's not a big deal to make a mistake in one project if VS doesn't transfer the mistake to all the other projects. But VS does, I loss the folders in all my past projects.

I tried create new project in Console or regular mode, I don't see the option of "Create directory for solution". I tries a few times already.

Also, I never see ADD-->New Filter option. They are all New Folder only.

Thanks
 
  • #23
yungman said:
I accidentally deleted the Source and Header Files(filter or folders) as described in the last post.
Maybe you should stop deleting stuff, accidentally or otherwise. You're like a bull in a china shop.
yungman said:
I accidentally deleted the folders, the Source and Header folders are ALL gone in ALL my projects. It's not a big deal to make a mistake in one project if VS doesn't transfer the mistake to all the other projects. But VS does, I loss the folders in all my past projects.
How are you determining this? If you're using Windows Explorer, you won't see any folders for Header Files and Source Files. If you're in VS, in Project Explorer, do you see these two "folders"? As already mentioned, these folders are just a way for VS to present the program hierarchy to you. If you work on a program with multiple projects, each project will have these folders.
yungman said:
I tried create new project in Console or regular mode, I don't see the option of "Create directory for solution".
Which version of VS are you using? If you're using VS 2017, which is what I'm using, this is what I'm talking about. If you're using VS 2019, I don't know what this dialog shows.
NewProj.png
 
  • #24
Thanks Mark

I am using VS2019.
When I create new project, these are the two page I get:
Create new project 1.jpg


Then when I click one of them, this will come up. There is no option for that. Only option is circled in red and it will not do it.
Create new project 2.jpg

It's not that I clumsily deleted the folders, it's the mouse/computer combination that when I choose say the .h file to delete, sometimes it will highlight the other stuffs, when I hit the delete button, it's too late.I know the same folders are gone in the other projects because when I open the old projects that were working and had the folders, they are gone. VS remember the actions I did and put it in other files. When I try to create new project, those folders are NOT there also. That's why I am so mad with VS. No other program I know of do that. Whatever you messed up, close the program and you can start over again. Not VS.

thanks
 
  • #25
You didn't answer the question I asked in the previous post:
Mark44 said:
How are you determining this? If you're using Windows Explorer, you won't see any folders for Header Files and Source Files. If you're in VS, in Project Explorer, do you see these two "folders"? As already mentioned, these folders are just a way for VS to present the program hierarchy to you. If you work on a program with multiple projects, each project will have these folders.
yungman said:
It's not that I clumsily deleted the folders, it's the mouse/computer combination that when I choose say the .h file to delete, sometimes it will highlight the other stuffs, when I hit the delete button, it's too late.
Well, if you're planning to delete something (but why you're doing this?), make sure that only what you want to delete is highlighted. It shouldn't be too late if you look first at what is going to be deleted.
 
  • #26
Mark44 said:
You didn't answer the question I asked in the previous post:
Well, if you're planning to delete something (but why you're doing this?), make sure that only what you want to delete is highlighted. It shouldn't be too late if you look first at what is going to be deleted.
The folder is gone in the VS Solution Explorer view. I could swear when I checked one of the old project and the header files folder was gone with the .h file and when I ran the compiler, it failed because it's missing the .h file. It was so fast I don't want to say it's 100%. But I am sure those folders are gone in the old projects where they were there before.

You don't know my mouse, it's crazy. I caught a lot, but somehow every so often, it slipped by me. My question is why VS is like this? Why when I made a mistake deleted the folder, it will reflect on all the other old projects and the new that are going to be created.

Thanks
 
  • #27
Actually after I resolved that ';' sarge, I got my whole program work easily. Lately, C++ is not giving me that much problem...Knock on wood. This is my complete program that I input 5 items with description, number of units and price. Then I can read all back, choose one of them and change and all store in a file in the computer.

This is Inventory.h.
C++:
#ifndef Inventory_H
#define Inventory_H
#include <iostream>
using namespace std;

class Inventory
{
private:
    int d_size = 31;
public:
        struct invItem
        {
            const int d_size = 31;
            char desc[31];
            int qty;
            double price;
        };
    void writeRec();
    void displayRec();
    void modify(long);
};
#endif // !Inventory_H
This is Inventory.cpp.
C++:
#include <iostream>
#include "Inventory.h"
#include <fstream>
#include <iomanip>
using namespace std;void Inventory::writeRec()
{
    fstream invFile;
    int index = 0;
    int numRec = 5;
    invItem record;
    cout << " Size of inventory = " << sizeof(record) << "\n\n";
    invFile.open("Inventory.dat", ios::out | ios::binary);
    for (index = 0; index < numRec; index++)
    {
        cout << " Record  " << index << endl;
        cout << setw(25) << left << " Description: ";
        cin >> record.desc; cout << endl;
        cout << setw(25) << left << " Quantity: ";
        cin >> record.qty; cout << endl;
        cout << setw(25) << left << " Price: " << "$";
        cin >> record.price; cout << "\n\n";
        invFile.seekp(index * sizeof(record), ios::beg);
        invFile.write(reinterpret_cast<char*>(&record), sizeof(record));
    }
    invFile.close();
    cout << " You are in writeRec.\n\n";
}
void Inventory::displayRec()
{
    fstream inven;    
    Inventory::invItem;
    invItem rec;
    long select = 0;
    inven.open("Inventory.dat", ios::in | ios::binary);
    inven.read(reinterpret_cast<char*>(&rec), sizeof(invItem));
    do
    {
        cout << " Record   " << select << endl;
        cout << setw(25) << left << " Description: " << rec.desc << endl;
        cout << setw(25) << left << " Quantity: " << rec.qty << endl;
        cout << setw(25) << left << " Price: " << "$" << rec.price << "\n\n";
        inven.read(reinterpret_cast<char*>(&rec), sizeof(invItem));
        select++;
    } while (!inven.eof());
    inven.close();
    cout << " You are in displayRec.\n\n";
}
void Inventory::modify(long index)
{
    char choose, again = 'n'; fstream inventory;
    invItem record; //It's object of struct invItem.
    inventory.open("Inventory.dat", ios::in | ios::out | ios::binary);
    inventory.seekg(index * sizeof(invItem), ios::beg);
    inventory.read(reinterpret_cast<char*>(&record), sizeof(invItem));
    cout << " Record  " << index << endl;
    cout << setw(25) << left << " Description: " << record.desc << endl;
    cout << setw(25) << left << " Quantity: " << record.qty << endl;
    cout << setw(25) << left << " Price: " << "$" << record.price << "\n\n";
    cout << " Do you want to change change record " << index << "? ";
    cin >> choose; cout << "\n\n";
    if (tolower(choose == 'y'))
    {
        cout << " Record  " << index << endl;
        cout << setw(25) << left << " Description: "; cin >> record.desc; cout << endl;
        cout << setw(25) << left << " Quantity: "; cin >> record.qty; cout << endl;
        cout << setw(25) << left << " Price: " << "$"; cin >> record.price; cout << "\n\n";
        //Display what you entered
        cout << " You entered new data in record " << index << endl;
        cout << setw(25) << left << " Description: " << record.desc << endl;
        cout << setw(25) << left << " Quantity: " << record.qty << endl;
        cout << setw(25) << left << " Price: " << "$" << record.price << "\n\n";
        //Write to file
        inventory.seekp(index * sizeof(record), ios::beg);
        inventory.write(reinterpret_cast<char*>(&record), sizeof(record));
        inventory.close();
        cout << " You are in modify.\n\n";
    }
}
This is source.cpp.
C++:
#include <iostream>
#include "Inventory.h"
using namespace std;
Inventory Inv;

int main()
{
    char again, choose;    int sel;
    //Write to record
    cout << " Do you want to create the record? "; cin >> choose;
    if (tolower(choose) == 'y')  Inv.writeRec();
    //Display all the records in the file
    cout << " Do you want to see all records? "; cin >> choose;
    if (tolower(choose) == 'y')  Inv.displayRec();
    //Read and edit particular file.
    do
    {
        cout << " What record you want to read and edit? ";    cin >> sel; cout << "\n\n";
        Inv.modify(sel);
        cout << " Do you want to edit another record? "; cin >> again;
    } while (tolower(again) == 'y');
    return 0;
}

My only question I cannot resolve is if you look at the Inventory.h file, I try to use const int d_size to declare the size of the char array. It just would not work, it just didn't like it when I put in private, public, in the source.cpp. I tried everything...Finally, I just put char desc[31] and the whole program work. How do you declare the size of a char array using a variable.

Thanks
 
  • #28
yungman said:
You don't know my mouse, it's crazy.
Then get another mouse.

yungman said:
I caught a lot, but somehow every so often, it slipped by me. My question is why VS is like this?
It's not. By your own admission you have been haphazardly deleting things at random.
yungman said:
Why when I made a mistake deleted the folder, it will reflect on all the other old projects and the new that are going to be created.
I don't see how VS could possibly do this.
 
  • Like
Likes Vanadium 50
  • #29
Mark44 said:
Then get another mouse.

It's not. By your own admission you have been haphazardly deleting things at random.
I don't see how VS could possibly do this.
No I did not say I delete things haphazardly, it was always the same problem.

I just copy two Solution explorer from project at least over a month old in chapter 6 and chapter 9:
Cpt 6 SE..jpg
Cpt 9 SE..jpg


Both folders are GONE. This is how VS work all along. You don't know that?

I never thought about changing the mouse because it is NOT a problem with any other programs That I use, everyone one of them just restart from the beginning when I close the program and re-open it. Or just as simple as Ctrl-Z to get back the deleted stuffs. Only VS remember what was done to it from before and there is no Ctrl-Z. Like I said, I use plenty of programs from free ones to 10K+ programs through out the years, never have I once encounter this. Don't tell me this one is special. I ran programs with 3D electromagnet simulations, don't tell me those are not as sophisticate as this one.

Also, all the other programs, when I run something, they always start from scratch, never have I even encounter a program that will screw up, have to Rebuild Solution or Clean Solution to reset and clear things or else it will give funny result. It's to the point I automatically rebuild solution before I run Ctrl-F5.Just go to any Window explorer, delete a file, hit Ctrl-Z, the file will come back. Just not in VS.
 
Last edited:
  • #31
Jarvis323 said:
Have you seen this yet?
https://www.physicsforums.com/threads/today-i-learned.783257/page-137#post-6398917
Turns out Bulls are pretty graceful in china shops. lol
Hi Jarvis

I know you don't use VS, what IDE are you using? Does it have free download version? I want to take a second look at other options as I have to spend a lot more time on VS as I get deeper in C++. On top of deleting folders, like you said, it is very slow for doing very simple job and the projects are so so huge. I don't know what they put in it.

thanks
 
  • #32
yungman said:
No I did not say I delete things haphazardly,

Post #16 -
yungman said:
But, of cause, I screwed up, I managed to delete both the Header Files and Source Files folders.
Post #16 -
yungman said:
I have to delete the .h from Header Files folder and put it in the Source Files folder as shown below.
Post #21 -
yungman said:
It is not that I want to create new folders( filters), I accidentally deleted them. My mouse/computer is funny,
These sound pretty haphazard to me.
yungman said:
That's really not a serious problem if I can just create a new project and copy all my .h and .cpp files over. BUT the STUPID VS...I repeat...THE STUPID VS remember what I deleted.
An old saying, "It's a poor craftsman who blames his tools" - https://idioms.thefreedictionary.co...n taking responsibility for their own failure.
Are you copying all of the files, including the ones that VS uses to layout the structure in Solution Explorer?
Or are you creating new projects from scratch?
If you are reusing the .vxproj and .vxproj.filters files from an older project, where you deleted a bunch of stuff, and copying them to a new project, then the new project is going to have missing folders as well.
yungman said:
that when I choose say the .h file to delete, sometimes it will highlight the other stuffs, when I hit the delete button, it's too late.
Like I said before, if you're about to hit the delete button, you need to look at what you're about to delete.
Furthermore, why are you deleting all these files? VS allows you to Remove a file (which deletes it) or Exclude from Project, which doesn't delete it, but just keeps it from participating in the build.
yungman said:
I never thought about changing the mouse because it is NOT a problem with any other programs That I use, everyone one of them just restart from the beginning when I close the program and re-open it.
Restarting from the beginning is not a function of the mouse. If the mouse isn't working right for you with the computer you have, then you either need to adjust the settings on the mouse, or get one that works well for you. I think you're working with a laptop. If the screen is too small for you to see well, then maybe you should get an external monitor so you can see things better. A lot of programmers work with large screens, up to four or five of them, and only work on laptops for making small changes to code they've already written.
yungman said:
Like I said, I use plenty of programs from free ones to 10K+ programs through out the years, never have I once encounter this. Don't tell me this one is special.
As someone told you in another thread, using an application is very different from writing an application. The fact that an application was free or cost $10,000 is irrelevant.
I've been using VS for 23 years, and I have never run across any of the problems that you are running into.
 
  • #33
yungman said:
Hi Jarvis

I know you don't use VS, what IDE are you using? Does it have free download version? I want to take a second look at other options as I have to spend a lot more time on VS as I get deeper in C++. On top of deleting folders, like you said, it is very slow for doing very simple job and the projects are so so huge. I don't know what they put in it.

thanks
I'm usually not using an IDE. Lately I've been mostly using Sublime Text (a fancy text editor), and various command line tools. The OS I use is a version of Linux.

If you want to develop in Windows without an IDE, you could use a text editor and use the compiler (cl) directly from the command line. I guess that will be a hurdle because you need to learn to use the command line. It might be a good learning experience though, because you can learn what VS actually is and what it is doing. You'll understand why you had the problems you had, when you know what the compiler is and how it works.

You could also try Qt creator, which is an IDE and GUI library. But I'm not sure it will be that much easier than VS.

Most developer tools are free.

You might be better off sticking with VS and keeping it simple though, and be careful to stop breaking things. Focus on learning C++ at this stage rather than all of the features of VS. It's up to you.
 
  • Like
Likes yungman
  • #34
Hi Jarvis

Thanks, I am not worry about how hard it is to learn, unless there is a way to recover things that is deleted, It is very inconvenient that whenever I screw up, it will affect all the older projects I did in the pass and everything I'll be doing in the future. Like I said, it's not that I am no careless, the new mouse and computer combination jump all the time. It's like I can type on this line and the cursor might jump somewhere else all of a sudden.

It is not a big problem if Ctrl-Z can recover as the rest of the program including Windows file explorer. The worst is even if I exit VS, the damage is done on the past projects and the future ones. How can VS don't have an option to NOT taking changes beyond the current project, that if I delete a folder, the same folder disappears in ALL my old projects like I showed in post 29.

Maybe you have idea how to prevent anything done to one project to affect all the other projects. That would solve all the problems. Worst case is just create a new undamaged project, copy all the files in and delete the damaged project.

Thanks
 
Last edited:
  • #35
Mark44 said:
Post #16 -
Post #16 -
Post #21 -
These sound pretty haphazard to me.
An old saying, "It's a poor craftsman who blames his tools" - https://idioms.thefreedictionary.com/a+poor+craftsman+blames+his+tools#:~:text=a poor craftsman blames his tools proverb Said,rather than taking responsibility for their own failure.
Are you copying all of the files, including the ones that VS uses to layout the structure in Solution Explorer?
Or are you creating new projects from scratch?
If you are reusing the .vxproj and .vxproj.filters files from an older project, where you deleted a bunch of stuff, and copying them to a new project, then the new project is going to have missing folders as well.
Like I said before, if you're about to hit the delete button, you need to look at what you're about to delete.
Furthermore, why are you deleting all these files? VS allows you to Remove a file (which deletes it) or Exclude from Project, which doesn't delete it, but just keeps it from participating in the build.
Restarting from the beginning is not a function of the mouse. If the mouse isn't working right for you with the computer you have, then you either need to adjust the settings on the mouse, or get one that works well for you. I think you're working with a laptop. If the screen is too small for you to see well, then maybe you should get an external monitor so you can see things better. A lot of programmers work with large screens, up to four or five of them, and only work on laptops for making small changes to code they've already written.
As someone told you in another thread, using an application is very different from writing an application. The fact that an application was free or cost $10,000 is irrelevant.
I've been using VS for 23 years, and I have never run across any of the problems that you are running into.

You actually stop and delete the Header Files in one project and look at your older projects to see whether the Header Files is gone? Read my post 29, I did not open those project for over a month, it had at least Source Files folder at the time because I always Add-->New source .cpp into the Source Files folder. The folder is gone in both that I opened. Do you have a way to prevent any accident from rippling to other projects?

NO, accident delete something is NOT a big deal if Ctrl-Z can recover. Usually the worst case is create a new project and copy over the files and delete the damage project. But VS do not allow you to do that, the damage will follow to the new project and affect all the old project just sitting there.

I am surprised you don't know that. I notice that in the first installment of VS2019, then there were too many problems and from you advice, I delete and reinstalled VS2019 the second time like 3 weeks ago. It sure works better. BUT this changes in one project ripple to other projects are always there...Of cause I could have done something wrong again.

Yes, this is a NEW project I just created to show you. No Header Files and Source Files folder. This is all I have:
New project.jpg


VS remember I deleted the folders and it WOULD NOT show up when I create a new project.
 
Last edited by a moderator:

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
Replies
10
Views
960
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
818
  • Programming and Computer Science
Replies
6
Views
922
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top