Shipping label code issue

Hey all, I'm pretty new to the programming world, and I need help with this one program. It requires that the user inputs their type of mail and calculates the cost of it based off the type and weight, their name and address, takes a checksum based off the zipcode, converts the zipcode into a barcode and displays it like this in the output;

******************************Postage Cost

First Name Last Name
1234 Street Name
City, State Zipcode

| Barcode |

Here's what I have so far but I'm getting errors about "invalid conversion from const char* to int" and I have no idea what that means.

Please help!



//This program formats a shipping label with a pseudo barcode for printing and placing on a package.
#include<iostream>
#include<string>
using namespace std;

int main()
{
string name, streetAddress, city, state; //To hold user location input information.
int zipcode, pseudocode, weight, postage, number; //To hold the user number input information.
const int cost1=0.49, cost2=0.98, cost3=2.54, addCharge1=0.22, addCharge2=0.20; //To hold the given constants.

//Gather user input data.
cout<<"Please enter your name:";
getline(cin, name);
cout<<"Please enter your street address:";
getline(cin, streetAddress);
cout<<"Please enter your city:";
getline(cin, city);
cout<<"Please enter your state:";
getline(cin, state);
cout<<"Please enter your zipcode:";
cin>>zipcode;
cout<<"Please enter 1 for letter, 2 for envelope or 3 for parcel:";
cin>>number;
cout<<"Please enter item's weight in ounces:";
cin>>weight;

//Calculate the cost of the item based off of type and weight.
if (number==1&&weight<=1)
cout<<"******************************"<<cost1<<"\n\n";
else
cout<<"******************************"<<cost1+(weight*addCharge1)<<"\n\n";
if (number==2&&weight<=1)
cout<<"******************************"<<cost2<<"\n\n";
else
cout<<"******************************"<<cost2+(weight*addCharge1)<<"\n\n";
if (number==3&&weight<=3)
cout<<"******************************"<<cost3<<"\n\n";
else
cout<<"******************************"<<cost3+(weight*addCharge2)<<"\n\n";

//The following will output the address information based off the user input.
cout<<name<<endl;
cout<<streetAddress<<endl;
cout<<city<<", "<<state<<" "<<zipcode<<"\n\n";

//The following converts the user input zipcode into a barcode.
string Barcode;
char digit;
{
if (digit == '0')
return "||:::";
else if (digit == '1')
return ":::||";
else if (digit == '2')
return "::|:|";
else if (digit == '3')
return "::||:";
else if (digit == '4')
return ":|::|";
else if (digit == '5')
return ":|:|:";
else if (digit == '6')
return ":||::";
else if (digit == '7')
return "|:::|";
else if (digit == '8')
return "|:::|";
else if (digit == '9')
return "|:|::";
else
return "Invalid";

}
string input_zip, result;
cout<<"Please re-enter your zipcode."<<endl;
cin>>input_zip;

pseudocode=input_zip(zipcode%10); //So as to add the 6th digit to the barcode as a checksum.

string final_Barcode=Barcode(pseudocode);
cout<<"| "<<final_Barcode<<" |"<<endl;
return 0;
}
main is of type int, and you try to return a string in it in all your if statements.

you probably want to do this:

string result = "Invalid";

...
if(...
result = "|:|::";

etc (skip the else assign invalid, its your default value in my example, hint..)


for your barcode block, then after all the if statements, print result.

return means to exit the current function (including main, when ends the program!).

the switch statement has a default, and may be more useful for this type of code in the future.


learning to read compiler error messages is critical to success.
when I try this with g++ I get

x.cpp:51:8: error: invalid conversion from 'const char*' to 'int'

which tells me the line number (use an editor that shows line numbers!!) (line 51) and the column (8, but I don't use this, its a hassle most of the time)
and the problem (trying to convert a char* (which is what a string is to the compiler, a pointer to characters or an 'array' of characters) into an integer (this isn't possible directly, you can't say int x = "35" in c++).

from there experience let me see the issue quickly, but understanding the message is super important.

Last edited on
Oh okay, I think I see what you're saying. This is what I have now for that part;

//The following converts the user input zipcode into a barcode.
string result;
char digit;
{
if (digit == '0')
result= "||:::";
else if (digit == '1')
result= ":::||";
else if (digit == '2')
result= "::|:|";
else if (digit == '3')
result= "::||:";
else if (digit == '4')
result= ":|::|";
else if (digit == '5')
result= ":|:|:";
else if (digit == '6')
result= ":||::";
else if (digit == '7')
result= "|:::|";
else if (digit == '8')
result= "|:::|";
else if (digit == '9')
result= "|:|::";
else
result= "Invalid";

}
string input_zip;
cout<<"Please re-enter your zipcode."<<endl;
cin>>input_zip;

pseudocode=input_zip(zipcode%10); //So as to add the 6th digit to the barcode as a checksum.

string final_Barcode=result(pseudocode); //Line 81, 79 is above
cout<<"| "<<final_Barcode<<" |"<<endl;
return 0;
}

Now I got those previous conversion errors cleared and now I'm down to two errors;

79 33 C:\Users\Christopher Mangum\Documents\School\C++\project2_chris_mangum.cpp [Error] no match for call to '(std::string {aka std::basic_string<char>}) (int)'

for both lines 79 and 81, shown in the comment. I've never even seen that one before and I've seen a lot of error codes being a total newbie.
Topic archived. No new replies allowed.