I have a console app which takes an input. The user inputs a text such as "It's better to be a '<'good man'>' rather than a '<succesful man'>" , and transforms it like so : "It's better to be a '<'GOOD MAN'>' rather than a '<SUCCESFUL MAN'>".
I did the following script but beeing C++ newbie it doesn t quite work and I get the following output:"It's better to be a '<good man'>' rather than a '<SUCCESFUL MAN'>"
Note:the program can have multiple "quotes".
This is my attept :
#include <iostream>
#include <cstring>
using namespace std;
char a[]="It's better to be a <good man> rather than a <succesful man>",*p;
int x,y,bec1,bec2;
int main()
{
for(int i=0;i<strlen(a);i++){
if(a[i]=='<'){
x=i;
bec1=1;
}
if(a[i]=='>'){
y=i;
bec2=1;
}
if(bec1==1 && bec2==1)
for(int j=x+1;j<y;j++){
if(a[j]!=' ')
a[j]-=32;
if(j==y){
bec1=0;
bec2=0;
}
}
}
cout<<a;
return 0;
}
Related
I would ask for your help and apologise if the question doesn't make sense.
I have a Microsoft Visual Studio project which I want to execute it multiple times in one go, and every time I will change one parameter.
Please see below the concept:
#include <iostream>
using namespace std;
int size_list = 2;
int my_list[2] = { 5, 6 };
int main()
{
for (int i = 0; i < size_list; i++)
{
cout<<"the number is "<<my_list[i]<<endl;
}
return 0;
}
So, I would like to replace the loop and instead I will have each element of my_list as parameters.
Is there any way to do so?
Thanks
You can pass the parameters in main().
int main(int argc, char* argv[]){std::cout<< argv[1];}
And use script like
for ($i=0;$i -lt 5; $i++)
{
#your CPP project and parameters array
Start-Process test.exe $arr[0]
Wait-Process test
}
I am working on a program that collects input from users and creates a list with results. The program also has to make a count of the choices and make a simple calculation.
So far I have this:
#include <iostream>
using namespace std;
struct emp{
char name[20];
char choice[20];
}obj[2];
int main(){
int i;
for(i=0;i<8;i++){
cout<<"Enter guest "<<i+1<< " Enter choice vegeterian/non-vegeterian: \n";
cin>>obj[i].name>>obj[i].choice;
}
cout<<"Table of Guests: \nNAME\tCHOICE\t\n";
for(i=0;i<8;i++){
cout<<obj[i].name<<"\t"<<obj[i].choice<<"\t\t"<<"\n";
}
return 0;
}
This creates the list needed in a simple way but I can not figure out how to do the count. I have tried using the if/else statement with the obj variable but it didn't work. Any tips or solutions will be greatly appreciated.Thanks
im doing a college project, it has to do with doing a program that manages orders of a pizzeria. So i have it all planned, but when it comes to doing it i missunderstand somethings. The first is that i need to ask the user for a ingredient, and in the program, the ingredients are a enum type data, so i need to match de keyboard input with that. I tried this
#include <iostream>
#include <array>
#include <sring>
#include <cctype>
#include <cmath>
#include <locale>
#include <algorithm>
using namespace std;
const int MAX_INGREDIENTES_PIZZA=20;
typedef array<int, MAX_INGREDIENTES_PIZZA> TIngrediente;
//enum ingredientes {TOMATE,QUESO,NATA,CEBOLLA,POLLO,HUEVO,SALAMI,ANCHOA,BACON,GAMBA};
TIngrediente leer_ingrediente(){
TIngrediente r;
enum ingredientes {TOMATE,
QUESO,
NATA,
CEBOLLA,
POLLO,
HUEVO,
SALAMI,
ANCHOA,
BACON,
GAMBA};
char ingrediente;
getline(cin,ingrediente);
for (int i=0; i<MAX_INGREDIENTES_PIZZA-1;i++){
string ingrediente[i]=tolower(ingrediente[p]);
}
if(ingrediente=="queso")
else if(ingrediente=="nata")
else if (ingrediente=="cebolla")
else if (ingrediente=="pollo")
else if (ingrediente=="huevo")
else if (ingrediente=="salami")
else if (ingrediente=="anchoa")
else if(ingrediente=="bacon")
else if (ingrediente=="gamba")
return r;
}
i dont know if i should declare enum{} as global, or just inside leer_ingredientes()
Another thing is that as i said i dont know what to do here (to match the input with the enum()) :
char ingrediente;
getline(cin,ingrediente);
for (int i=0; i<MAX_INGREDIENTES_PIZZA-1;i++){
string ingrediente[i]=tolower(ingrediente[p]);
}
if(ingrediente=="queso")
else if(ingrediente=="nata")
else if (ingrediente=="cebolla")
else if (ingrediente=="pollo")
else if (ingrediente=="huevo")
else if (ingrediente=="salami")
else if (ingrediente=="anchoa")
else if(ingrediente=="bacon")
else if (ingrediente=="gamba")
return r;
Can anybody help me?
Well good practice is to write a function to convert string to ingrediente.
which will look like below. Assuming that enums have basic type int with your platform
int strToIngrediente(const char *ingr)
{
if(!strcmp(ingr, "queso"))
return QUESO;
if(!strcmp(ingr, "queso"))
return QUESO;
if(!strcmp(ingr, "nata"))
return NATA;
.
.
.
}
My program without bit that crashes that works fine just as I want it to:
#include <iostream>
#include <string>
#include <fstream>
#include <map>
typedef std::map<int,std::string> strlist;
strlist get_program(std::string RELPATH)
{
std::ifstream file(RELPATH);
strlist out;
for(int i = 0; i < 3; ++i)
{
std::getline(file,out[i+1]);
}
return out;
}
int main()
{
strlist program=get_program("program.txt");
strlist tokenlist=get_tokens(program[1],' '); //in the "version of the program that doesn't crash",
//this line is removed due to the functions it is directly and indirectly referring to being removed
std::string in,out;
std::cout<<"[Cyan 0.1]\n";
std::cin>>in;
for(int i=1;i<=program.size();i++)
{
std::cout<<program[i]<<"\n";
}
std::cin>>in;
return 0;
}
The part whose existence in the program somehow causes the crash (it is put right before the main function):
bool checkforchars(std::string chars,char c) //Returns true if one of char's in given string is identical to given char c
{
for(int i=0; i<chars.length();i++)
{
if(chars[i]==c) {return true;}
}
return false;
}
strlist get_tokens(std::string in,char tokenCut)
{ //Returns an ordered (by occurence) list of strings with each string containing a single token. The separation of tokens is decided by the single character specified by tokenCut
strlist tokens;
int token_amnt=1;
for(int i=0;i<in.length();i++)
{
if(in[i]==tokenCut)
{
token_amnt++;
}
else
{
tokens[token_amnt]=tokens[token_amnt]+in[i];
}
}
}
I am making an interpreter (I'm calling it "Cyan") and the program without the "crashy" bit works fine and prints the lines of program.txt once I've given it some input (first input doesn't actually do/affect anything other than delay the next bit until the user inputs). If I add the "crashy" bit along with the line "strlist tokenlist=get_tokens(program[1],' ');" then it crashes, but there's two weird things:
It only crashes the moment "return 0;" is executed (I know this because the last input is so that I can get the program to close when I like, and when I give it that last input it crashes)
It does the same thing even if I remove the "strlist tokenlist=get_tokens(program[1],' ');" line, which implies that the existence of the "crashy functions" in the program is the cause of it crashing when the program ends
If it crashed the moment that line involving the "crashy functions" executed then it would've made a lot more sense, although I'd still be equally clueless about what is making it crash. I am using MinGW for Windows 7 (32-bit) and compiling using
g++ main.cpp -std=c++11 -o program.exe
if that helps in any way.
So I made the following algorithm to solve a problem, but I don't understand why it crashes when I use fstream instead of iostream. When I use lower numbers, for example (input e.g.: 1 4 5 ) it works perfectly with fstream, but if I use some larger input numbers (e.g: 3 987654300 210 ),it says "Process returned -1073741819 (0xC0000005)". If I use iostream, it works great even with 9 digit inputs.
Yes, I have the parola.in and parola.out in the same folder with the .cpp/.exe and the .in has the input numbers and everything.
Ignore the algorithm and the comments, and just pay attention at the use of "fout" and "fin" please.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin("parola.in");
ofstream fout("parola.out");
int a,b,k; //VARIABILE ORIGINALE
int ord=1; //NUMARUL DE ORDINE AL VECTORILOR
int cifa=0,cifb=0; //NUMARUL DE CIFRE AL NUMERELOR A SI B
int nrA[18],nrB[10]; //VECTORII CE CONTIN CIFRELE NUMERELOR A SI B
int temp[9]; //VECTORUL CE CONTINE CIFRELE NR. A IN ORDINE INVERSA
fin>>k>>a>>b;
while (a>0)
{
temp[ord]=a%10;
a/=10;
ord++;
cifa++;
}
ord=1; //RESET NR ORDINE
while (b>0)
{
nrB[ord]=b%10;
b/=10;
ord++;
cifb++;
}
for (int topkek=1; topkek<=cifa; topkek++)
{
nrA[topkek]=temp[cifa+1-topkek]; //ORDONAREA CIFRELOR LUI A CRESCATOR, IN VECTORUL nrA
}
for (int zomtan=1; zomtan<=k; zomtan++)
{
fout<<nrA[zomtan];
nrA[zomtan]=-1;
}
fout<<nrB[1];
nrB[1]=-1;
for (int zomtan2=cifa+1; zomtan2<=18; zomtan2++)
{
nrA[zomtan2]=-1;
}
for (int zomtan3=cifb+1; zomtan3<=10; zomtan3++)
{
nrB[zomtan3]=-1;
}
for (ord=1; ord<=9; ord++)
{
if (nrA[ord+k]!=-1)
{
fout<<nrA[ord+k];
}
if (nrB[ord+1]!=-1)
{
fout<<nrB[ord+1];
}
}
return 0;
}
The problem can occurs beause, as I suppose, you use 1 based arrays instead of propper 0 based
for (int zomtan2=cifa+1; zomtan2<=18; zomtan2++)
{
nrA[zomtan2]=-1;
}
the problem will always occurs because you have wrote zomtan2<=18
and your array declaration is int nrA[18] wich I have said previously always casues problems.
These errors are everywhere in your code, so i suggest you to rewrite the program