How should i solve this task? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I got the homework,i try to solve it,but i dont know why its bad.
One of part of this code was a default:
#include <iostream>
using namespace std;
/*##SOLUTION##*/
/*##SOLUTION##*/
int main()
{
Valami var;
int i=var.ujszam();
cout<<i<<endl;
cin>>i;
var.tarol(i);
var.print();
return 0;
}
I have to to supplement this code with some criterion.
I have to create Valami class after that I have to create ujszam method which is get the integer number and give it back.
I have to create tarol method which is save the Integer number.
and finally i have to create print method which is display the saved number.
The input -2 and 72,the result gonna be -2 and "A szam : 72"
Here is my attempt:
class Valami
{
public:int tarol;
public:int ujszam()
{
int i;
cin>>i;
return i;
}
public:void tarol(int ertek)
{
tarol=ertek;
}
public:void print()
{
cout << "A szam : " <<tarol<< endl;
}
};
I try to fix it some many ways,but idk what is the problem.

tarol is used as name of both a variable and a function. You have to give another name to one of them.
example:
class Valami
{
public:int tarol_var; // rename the variable
public:int ujszam()
{
int i;
cin>>i;
return i;
}
public:void tarol(int ertek)
{
tarol_var=ertek; // rename the variable
}
public:void print()
{
cout << "A szam : " <<tarol_var<< endl; // rename the variable
}
};

Related

Why won't the string push or pop in stack [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This is an assignment given to me.
I am a noob just started programming.
The string as a whole won't push onto the stack and how to pop it.
Problem statement:- divide a whole string consisting of a full name, divide the string into 3 parts to get the first name middle name and surname and display them in the order of surname first name middle name USING STACKS ONLY.
I've tried using 2D stack
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string>
using namespace std;
using std :: string;
char s1[100];
char s2[50],s3[50],s4[50];
int i=0,j=0,k=0,max1=9,top=-1;
char stack[10][10];
char re[10];
void push(char val[])
{
if(top>=max1)
{
cout<<"Stack overflow";
}
else
{
top++;
int a=0;
for (int i=0;i<stack[top]['\0'];i++)
{
stack[top][a]=val[i];
a++;
}
}
}
char* pop()
{
if(top<0)
{
cout<<"Stack underflow";
}
else
{
//for(int j=0;j<=top)
for(int i=0;i<stack[top]['\0'];i++)
{
re[i]=stack[top][i];
top--;
return re;
}
}
}
void divstring()
{
for(i=0;s1[i]!=' ';i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
i++;
while(s1[i]!=' ')
{
s3[j]=s1[i];
j++;
i++;
}
s3[j]='\0';
i++;
while(s1[i]!='\0')
{
s4[k]=s1[i];
k++;
i++;
}
s4[k]='\0';
i++;
}
int main()
{
//clrscr();
cout<<"Enter the string: ";
gets(s1);
divstring();
cout<<"The 1 part is "<<s2<<endl;
cout<<"The 2 part is "<<s3<<endl;
cout<<"The 3 part is "<<s4<<endl;
// getch();
push(s1);
push(s2);
push(s3);
cout<<pop();
return 1;
}
There are no compile time errors but the strings don't get pushed onto the stack nor popped.
There's multiple problems here.
There is no such thing as a 2D stack. A stack is one dimensional. It's got one usable end. In C++, you use std::stack, push(), pop() and empty().
You need to get rid of the global variables.
You seem to want to implement your own stack. You need to decide, do you want to implement a stack, that is one question. Or, do you want to use an existing stack to perform the assignment? that's a different question. I can't tell between the two.
You should use std::string.
You don't handle boundary condition. For example passing a string without spaces will loop infinitely in
for(i=0;s1[i]!=' ';i++)
The push function is very confused, here's a working version
void push(char val[])
{
if (top >= max1)
{
cout << "Stack overflow";
}
else
{
top++;
int i = 0;
for (; val[i] != '\0'; i++)
stack[top][i] = val[i];
stack[top][i] = '\0';
}
}
There are many ways you could improve this, you could use the C strcpy function instead of copying your strings by hand. Or, heaven forbid, you could use some C++, like std::string and std::stack.

What happens when an object is assigned to a value in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Check the following code:
#include<iostream>
using namespace std;
class example
{
public:
int number;
example()
{
cout<<"1";
number = 1;
}
example(int value)
{
cout<<"2";
number = value;
}
int getNumber()
{
cout<<"3";
return number;
}
};
int main()
{
example e;
e = 10;
cout<<e.getNumber();
return 0;
}
What is the output of the above code. Also, I want to know what happens when an object is directly assigned to a value. How will the compiler interpret it?
first you typed
example e;
So first constructor called and 1 printed
example()
{
cout<<"1";
number = 1;
}
output :
1
then you typed :
e=10 its equal to e = example(10); so another constructor called :
example(int value) /// beacause you used example(10)
{
cout<<"2";
number = value;
}
so your output is :
12
and number is 2
Finally in :
cout<<e.getNumber();
3 is couted but in the other hand value is `10`
because number = value your number is 10
So in Finally your output is :
12310
thanx for #StoryTeller for editting explaintions

How to fix my program so that my functions can work? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For HW (intro to C++) I had to modify a program that I wrote previously, I will be including that code here. Seeing the output of it will make things much more clearer:
//Program to compute and display the average and appropriate letter grade of 3 test scores
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
char grade;
double Test_1,Test_2,Test_3,Avg,ClassAvg1,ClassAvg2,ClassAvg3;
int sumTest_1,sumTest_2,sumTest_3;
int n;
sumTest_1=sumTest_2=sumTest_3=0;
const int totalSum=5.0;
for(n=1;n<=5;n++) {
do {
cout<<"What are the three test scores for student #"<<n;
cin>>Test_1>>Test_2>>Test_3;
if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)
cout<<"You entered an invalid score - please try again"<<endl;
}
while(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100);
Avg=((Test_1+Test_2+Test_3)/3.0);
if(Avg<65) {
grade= 'F';
}
else if(Avg<70)
{
grade= 'D';
}
else if(Avg<80)
{
grade= 'C';
}
else if(Avg<90)
{
grade= 'B';
}
else
{
grade='A';
}
cout<<setprecision (0)<<fixed;
cout<<"Your test average is "<<Avg<<" and your grade is " <<grade<<endl;
sumTest_1=sumTest_1+Test_1;
sumTest_2=sumTest_2+Test_2;
sumTest_3=sumTest_3+Test_3;
ClassAvg1=sumTest_1/5.0;
ClassAvg2=sumTest_2/5.0;
ClassAvg3=sumTest_3/5.0;
}
cout<<"The class average for test #1 is: "<<ClassAvg1<<endl;
cout<<"The class average for test #2 is: "<<ClassAvg2<<endl;
cout<<"The class average for test #3 is: "<<ClassAvg3<<endl;
}
For hw, I have to modify the program above by including two functions, ComputeAvg which will be called to compute and return the student's average and LetterGrade, to compute and return the letter grade. This is so that instead of using one main program, I now use these 2 functions. I have written this code so far, but I'm not sure if I'm doing it right and it will be great if someone can explain things out for me since I'm still not very comfortable writing functions:
//Program to compute and display the average and appropriate letter grade of 3 test scores
#include<iostream>
using namespace std;
void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
double Avg;
double ComputeAvg(int Test_1,int Test_2,int Test_3);
{
Avg=ComputeAvg(Test_1,Test_2,Test_3);
return(Test_1+Test_2+Test_3)/3.0;
}
}
int main()
{
int Test_1,Test_2,Test_3,n;
double Avg;
for(n=1;n<=5;n++)
{
do
{
cout<<"What are the three test scores for student #"<<n;
cin>>Test_1>>Test_2>>Test_3;
if(Test_1<1||Test_1>100||Test_2<1||Test_2>100||Test_3<1||Test_3>100)
cout<<"You entered an invalid score - please try again"<<endl;
}
cout<<"Your test average is:"<<Avg<<endl;
ComputeAvg(Test_1,Test_2,Test_3);
}
}
#include<iostream>
using namespace std;
void LetterGrade(int Test_1, int Test_2, int Test_3)
{
double Avg;
double LetterGrade(int Test_1,int Test_2,int Test_3);
{
Grade=LetterGrade(Test_1,Test_2,Test_3);
void ComputeAvg(int Test_1, int Test_2, int Test_3)
{
double Avg;
double ComputeAvg(int Test_1,int Test_2,int Test_3);
{
Avg=ComputeAvg(Test_1,Test_2,Test_3);
return(Test_1+Test_2+Test_3)/3.0;
}
}
The above code doesn't look correct, especially using a nested function or recursion.
Something tells me you want a simplified version:
double Compute_Average(int test_1, int test_2, int test_3)
{
return ( static_cast<double>(test_1)
+ static_cast<double>(test_2)
+ static_cast<double>(test_3))
/ 3.0;
}
Since the function is returning a value, in your main function you want to store the result in a variable:
double Avg = Compute_Average(Test_1, Test_2, Test_3);
cout << "Test average is: " << Avg << "\n";

Adding a struct into an array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So lets say I have a struct like this:
struct example_structure
{
int thing_one;
int thing_two;
};
I also have an empty array which I am trying to fill with these structs. I am trying to add them as follows, but it doesn't seem to be working:
array[i].thing_one = x;
array[i].thing_two = y;
Instead of this is there a way to declare a variable of type example_structure and then add that to the array?
Use vectors. They can expand as needed.
#include <iostream>
#include <vector>
int main()
{
struct example_structure
{
int thing_one;
int thing_two;
};
std::vector<example_structure> data;
for (int i = 0; i < 3; i++)
{
data.push_back({i, i * 2});
}
for (const auto& x : data)
{
std::cout << x.thing_one << " " << x.thing_two << "\n";
}
}
Live example:
http://ideone.com/k56tcQ
You can write simply
array[i] = { x, y };
Or you can have a separate variable of the type of the structure. For example
struct example_structure obj = { x, y };
array[i] = obj;

Why am i getting the error "Array type Item[1000] is not assignable"? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm quite new to c++, I need a bit of help on this error,
'Array type Item[1000] is not assignable' ? I'm on XCode by the way,
I know I should be on Windows. This is a school homework, so if you
could guide me, without giving much away, that would be awesome. Oh and sorry
if I mess something up, this is my first post.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//global constant
const int MAX_SIZE = 1000;
//make an Item class
class Item
{
private:
string id;
int sold, remain;
public:
void set_id(string _id);
void set_sold(int _sold);
void set_remain(int _remain);
};
//all the methods setting id, sold, and remaining
void Item::set_id(string _id){
id = _id;
}
void Item::set_sold(int _sold){
sold = _sold;
}
void Item::set_remain(int _remain){
remain = _remain;
}
//code to read from file
ifstream inFile;
ofstream outFile;
//function that reads file to array
void read_In() **I think it's because this should not be a void**
{
//variables
int ct = MAX_SIZE;
Item a[MAX_SIZE];
//open the file
inFile.open( "inventory.txt" );
//while the file hasn't ended, read and place into array of Items
while( !inFile.eof() ){
string for_id = "";
int for_sold = 0;
int for_remain = 0;
for(int i = 0;i<ct;i++){
//store the next thing on the file to the array of Items
inFile >> for_id;
a[i].set_id(for_id);
inFile >> for_sold;
a[i].set_sold(for_sold);
inFile >> for_remain;
a[i].set_remain(for_remain);
}
}
inFile.close();
}
int main(){
Item sp[MAX_SIZE];
sp = read_In(); **Apparently the error is here!**
system("pause");
return 0;
}
First of all close comments properly. Half of errors will be go. Secondly, you are using,
void read_In(){
So read_In returns void.. then you cannot take it's value, sp = read_In(); is wrong.