I'm trying to make a little "Database" program for the stuff I eat and drink, when I input the name of the product I want a list to pop out and tell me the ingredients.

HOWEVER. I don't want to write one big program for it because I am expecting this list to grow and I don't want to have to wait on compilation times for any little change to any product. I would like to implement every product with a different function, and then call that function from my main.cpp file through headers. Please help me if you can. thanks.

My Code: main.cpp

 #include <iostream>
 #include <limits>
 #include "Arizona_Green_Tea.h"

using namespace std ;



int main(){
    char input[100] ;

    std::cout << "Search Database: " ;
    std::cin >> input ;

    if (input == "Arizona"){
        Arizona_Green_Tea();
    }



return 0 ;
}

Arizona_Green_Tea.cpp

#include "Arizona_Green_Tea.h"

int Arizona_Green_Tea()
{

}

Arizona_Green_Tea.h

#include <iostream>
using namespace std ;

int Arizona_Green_Tea(){
    std::cout << "Premium Brewed Green Tea " << std::endl ;
    std::cout << "Filtered Water" << std::endl ;
    std::cout << "High-Fructose Corn Syrup" << std::endl ;
    std::cout << "Honey" << std::endl ;
    std::cout << "Citric Acid" << std::endl ;
    std::cout << "Natural Flavors" << std::endl ;
    std::cout << "Ginseng Extract" << std::endl ;
    std::cout << "Ascorbic Acid" << std::endl ;
 }

How can I connect these files? Or what can I do to make my program work? Thanks!

有帮助吗?

解决方案

Your header should have a prototype of the function, i.e. a function without a body:

// This goes into the header
int Arizona_Green_Tea();
// Note that there are no curly braces here; your header has a pair of curly braces,
// meaning that there's an empty body.

Currently, your function has a body, so you end up with duplicate function definitions.

I still can't see the output, after I write "Arizona", it just stays blank

Here is the culprit:

char input[100] ;

It is because of this declaration that the comparison below did not work:

if (input == "Arizona") ...

Include the <string> header, and change the declaration to std::string, like this:

string input;

其他提示

Abstract Factory Design Pattern seems like the route you want to take based on your question. Essentially it means you create an abstract class, let's say "Food", that has an int constructor and allow it to be inherited by your newly created classes(such as Arizona_Green_Tea). You would need to manually add the header files in an include line like you did in main.cpp and rebuild, but there is no way getting around that in my opinion.

This is great for your Database program since you could later create functions that take in a "Food" class argument and you can pass in any of the inherited classes such as Arizona_Green_Tea.

Also, I am guessing you had a mistype when you listed Arizona_Green_Tea.h and Arizona_Green_Tea.cpp? The code for each as it is listed is not correct and would need to be switched.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top