Center a 17x11 paper using Horizontal - c++

#include "Calculator.h"
#include <iostream>
#include <string>
#include <cmath>
#include "DoWhileLoops.h"
#include "Average.h"
#include "SwitchStatement.h"
#include "LogicalOperators.h"
#include <cstdlib>
#include "RandomNumberGenerator.h"
#include <stdlib.h>
#include <ctime>
#include "DefaultArguments.h"
#include "FindingCenter.h"
using namespace std;
int length;
int width;
int Horizontal(int length, int width);
FindingCenter::FindingCenter()
{
cout << Horizontal();
cout << "What is the length?" << endl;
cin >> length;
cout << "What is the width?" << endl;
cin >> width;
}
int Horizontal(int length, int width)
{
return ((17-(length + width))/ 3);
}
I keep getting this error:
"too few arguments to function 'int Horizontal(int, int)"
the code is for my own personal use im trying to center a 17x11 paper.
its an orthographic projection paper for my mechanical drawing class... also any other pointers will help thx :)

The error is pretty clear: you are calling Horizontal with less than the required 2 arguments: in fact, you are calling it with no arguments.
Shouldn't you be calling it after you've read in length and width, so you can pass them to it?

Related

Error while performing string copy operation in C++

Please explain why is this giving an error but the other on is running fine
The following code gives the error:
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
string s1,s2;
int i;
cout << "Enter the string to copy into another string : ";
getline(cin,s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}
Error looks like this
But this works perfectly fine
#include <iostream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
char s1[100], s2[100], i;
cout << "Enter the string to copy into another string : ";
cin>>s1;
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}
In your case, s2 is initialized to an empty string with a length of 0, so you can't write past the bounds. If you want to, you must first resize it:
s2.resize(s1.length());
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
Also, c++ std::string does not need a terminating nullbyte, unlike C strings.

Difficulty understanding while loop execution

#include <iostream>
#include <iomanip>
#include <ios>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
using std::cin; // <iostream>
using std::cout; // <iostream>
using std::endl; // <iostream>
using std::setprecision; // <iomanip>
using std::sort; // <algorithm>
using std::streamsize; // <ios>
using std::string; // <string>
using std::vector; // <string>
int main()
{
cout << "Enter your homework grades : " << endl;
double x;
vector<double> homework;
int count = 0;
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0)
{
cout << "Error, enter a grade" << endl;
continue;
}
}
return 0;
}
Hi, I'm wondering why my while loop wont print the message (Error, enter a grade) on the screen if the if-statement is within the loop, and seems to only work when its placed outside after the loop executes why is this the case?
You are never going back to zero.
int count = 0;
//...
while(cin >> x)
{
homework.push_back(x);
++count;
if(count == 0) //count will never be zero

Still giving me a repeating vector

I am a new C++ programmer, I would like to ask you a simple question but I do not understand why output still give me repeating data:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream> // std::ifstream
#include <string.h>
#include <iostream>
#include <complex>
#include <sstream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
int nfft;
nfft = 256;
vector<float>f;
for(unsigned i=0; i<nfft; i++){
f.push_back(((i/(nfft-1))-0.5)*8e6);
}
for(unsigned i=0;i<nfft;i++){
cout << f[i] << endl;
}
}
My expectation is a vector: [-4.0000 -3.9686 -3.9373 -3.9059 -3.8745 -3.8431 -3.8118 -3.7804 -3.7490 -3.7176 -3.6863 -3.6549 ....... 3.9059 3.9373 3.9686 4.0000]
Please give me a hand to solve this problem.
Thank you so much.
(i/(nfft-1)) is well protected with parentheses and thus performs an integer division.
Since nfft > i you get zero => always the same value.
Example of how to ix:
f.push_back(((float(i)/(nfft-1))-0.5)*8e6)

C++ Error: No Member in Class?

I'm working on C++, and this is just a very basic program, but I'm still getting an error.
The error message is:
'class secondary' has no member named 'getting'.
Why is this? It works for my void setting, but not for getting? What am I doing wrong here?
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
string getting();
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if(factor < 3){
result = "You have a very low self esteem.";
}elseif(factor > 3){
if(factor > 7){
result = "You have a very high self esteem."
}else{
result = "You have a medium self esteem."
}
}
return result;
}
private factor;
Actually, looking at this again, and deeper, this code has many issues (semicolons missing at key points and the private int definition should have been in the header file, not the cpp file 9t(private is its own section, see below):The problem, from what I can see, s has not yet been instantiated yet, do so and the operation should work correctly.
Please also note that when factor was defined in the cpp file, it was defined at bottom, it should actually be defined before any use of the variable to be defined (in the header file is better meet with common/conventional coding standards).
Please check this tested code:
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
std::string getting();
private: // Note: this is how you do private
int factor; // This is the definition with type int, missing in original
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if (factor < 3){
result = "You have a very low self esteem.";
}else if(factor > 3){
if (factor > 7){
result = "You have a very high self esteem.";
}
else{
result = "You have a medium self esteem.";
}
}
return result;
}

Dice roll not working C++ deafult_random_engine

For some reason I keep getting 6 every time. I know of another way to do a random dice roll, but I wanted to learn how to use the deafult_random_engine.
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int main()
{
default_random_engine randomGenerator(time(0));
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
}
But this bit of code works with the time(0).
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
// dice roll
{
srand(time(0));
for(int x = 1; x < 2; x++){
cout << 1+(rand()%6) << endl;
}
return 0;
}
It's almost certainly the use of time(0) as the culprit here.
You should probably opt for a method like this:
#include <iostream>
#include <string>
#include <chrono>
#include <random>
#include <ctime>
using namespace std;
int main() {
default_random_engine randomGenerator(std::random_device{}());
// OR:
// default_random_engine randomGenerator(
// (unsigned) chrono::system_clock::now().time_since_epoch().count());
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
return 0;
}
While your original code always produced 6 on my system, this one seems a little more "adventurous":
pax> for i in {1..10}; do ./qq ; sleep 1 ; done
You rolled a 5
You rolled a 5
You rolled a 6
You rolled a 1
You rolled a 6
You rolled a 5
You rolled a 2
You rolled a 3
You rolled a 5
You rolled a 4
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int main()
{
mt19937 randomGenerator(time(0));
uniform_int_distribution<int> diceRoll(1, 6);
cout << "You rolled a " << diceRoll(randomGenerator) << endl;
}