Ive written some code for a buyable link for my website. When I perform the same steps in google sheets it works perfectly every time, but when i wrote the application something isn’t working and I’m at a bit of a loss. The code was reverse engineered from another free program i was using that has errors in the output all the time and in an effort to solve this problem I decoded the output and it (as far as i can tell) is json in this format {“products”:[{“id”:”intern. id #”,”quantity”:”#”,”options”:{“type”:”option name”}}]} with either the last two characters removed and a coma as a delimiter between the next set of this same template added i.e.{“products”:[{“id”:”intern. id #”,”quantity”:”#”,”options”:{“type”:”option name”}},{“products”:[{“id”:”intern. id #”,”quantity”:”#”,”options”:{“type”:”option name”}}]}. Then all of this is percent coded so it would look something like %5B%7B%22id%22%3A%22intern.+id+%23%22%2C%22quantity%22%3A%22%23%22%2C%22options%22%3A%7B%22type%22%3A%22option+name%22%7D%7D%2C%7B%22products%22%3A%5B%7B%22id%22%3A%22intern.+id+%23%22%2C%22quantity%22%3A%22%23%22%2C%22options%22%3A%7B%22type%22%3A%22option+name%22%7D%7D%5D%7D, and then that’s finally
encoded in base64 as i said i built this app in google sheets but its size is limited because google sheets can only be so big but i can do this very easily in c++ and qt and have a prety simple easy to use app. If i could figure out whats diffent. I should also say this code will open the cart but not fill it with anything or in some cases only fill it with the first thing in the list and im not understanding why
this is my driver code:
#include<iostream>
#include<fstream>
#include<sstream>
#include<list>
#include<string>
#include<base64.h>
#include<encoder.h>
#include<infoFind.h>
#include<escapencode.h>
using namespace std;
int main()
{
list <string> choices;
string done="";
string internid="";
string quant="";
string type="";
string option="";
string open="https://www.sustainhydro.com/online-store/!/~/cart/create=";
string body1="{"products":[{"id":"";
string body2="","quantity":"";
string body3="","options":{"";
string body4="":"";
string body5=""}}";
string close="]}";
string delim=",";
string temp="";
string comp="";
string input="";
while (done != "n")
{
catinfo();
cout<< "" << 'n';
cout << "Enter Internal Id: ";
cin >> internid;
cout << "Enter Quantity: ";
cin >> quant;
cout << "Enter Type: ";
cin >> type;
cout << "Enter Option: ";
cin.ignore();
getline(cin,option);
cout <<""<<'n';
cout << "Do you have another SKU: y , n : ";
cin >> done;
temp=body1+internid+body2+quant+body3+type+body4+option+body5;
choices.push_back(temp);
choices.push_back(delim);
if (done == "y")
{
continue;
}
if (done == "n")
{
choices.pop_back();
choices.push_back(close);
for (auto i : choices)
{
comp+=i;
}
if (!comp.empty() && (comp.back() == 'n' || comp.back() == ''))
{
comp.pop_back();
}
comp = escape(comp);
cout << comp << endl;
comp=base64_encode(comp);
cout << comp << endl;
cout << 'n';
cout << "THIS IS YOUR URL:" << endl;
cout << 'n';
cout << open;
cout << comp;
cout << "" << 'n';
cout << "" << 'n';
cout << "Copy and Paste the URL From Here";
cout << "" << 'n';
break;
}
}
return 0;
}
custom header files to follow :
escapeencode.h
#include<iterator>
#include<algorithm>
#include<iostream>
#include<string>
#include<list>
using namespace std;
string space="%20";
string colon="%3A";
string bracko="%5B";
string brackc="%5D";
string braceo="%7B";
string bracec="%7D";
string quote="%22";
string comma="%2C";
string escape(string json)
{
list<string>temp;
string result="";
char chr;
for(char i : json)
{
chr=i;
string newchr(1,chr);
if(newchr == "[")
{
newchr=bracko;
}
if(newchr == "]")
{
newchr=brackc;
}
if(newchr == " ")
{
newchr=space;
}
if(newchr == ":")
{
newchr=colon;
}
if(newchr == "{")
{
newchr=braceo;
}
if(newchr == "}")
{
newchr=bracec;
}
if(newchr == """)
{
newchr=quote;
}
if(newchr == ",")
{
newchr=comma;
}
temp.push_back(newchr);
}
for(string i : temp)
{
result=result+i;
}
return result;
}
base64.h
#include <algorithm>
#include <array>
#include <cctype>
#include <string>
const std::array<char, 64> base64_chars = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
std::string base64_encode(const std::string& input) {
std::string output;
int val = 0, valb = -6;
for (unsigned char c : input) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
output.push_back(base64_chars[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) output.push_back(base64_chars[(val << (6 + valb)) & 0x3F]);
while (output.size() % 4) output.push_back('=');
return output;
}
the same things are happening in my qt version and Ive used some of the code from this console application in my qt code as well if needed i can include that code as well i just didn’t want to be to long winded here and i feel like i probably already have. Also cards on the table Im not a networking specialist or a website designer and im not the best coder out there i know that as far as the industry is concerned Im a shit coder.
Wannabe_coder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.