Problem when executing a function - c++

I'm trying to run a simple C++ program (as I'm JUST starting to learn C++) and tried this example code off a website as a program that adds two numbers together. When I execute the program, I never get errors, but c always returns as 0. Help??
#include <iostream.h>
int Add (int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << "\n";
return 0;
}
int main()
{
std::cout << "I'm in main()!\n";
int a, b, c;
std::cout << "Enter two numbers here: ";
cin >> a;
cin >> b;
std::cout << "\nCalling Add()\n";
c=Add(a,b);
std::cout << "\nBack in main().\n";
std::cout << "c was set to " << c;
std::cout << "\nExiting...\n\n";
return 0;
}
Thanks in advance.

Since Add returns 0, c will always be 0. You need to, you know, actually add the numbers you pass into Add and return that from the function.

Give this a whirl. NOTE: Add returns (x+y)
#include <iostream>
using namespace std;
int Add(int x, int y)
{
std::cout << "In Add(), received " << x << " and " << y << std::endl;
return (x+y);
}
int main()
{
std::cout << "I'm in main()!\n";
int a, b, c;
std::cout << "Enter two numbers: ";
std::cin >> a;
std::cin >> b;
std::cout << "\nCalling Add()\n";
c=Add(a,b);
std::cout << "\nBack in main().\n";
std::cout << "c was set to " << c;
std::cout << "\nExiting...\n\n";
return 0;
}

Your Add function always returns 0, so it's a mystery why c is always 0? :) It should be return x + y.

Related

I dont know how to create a program that prints out my integers or strings in numerical/alphabetical order [duplicate]

This question already has answers here:
Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?
(5 answers)
Closed 3 years ago.
I dont know how to make this book exercise that wants me to print out three integers or three strings in numerical/alphabetical order.
I've tried using if statements to solve this but it just fails because im a beginner.
with code like this if(a
cout << a << b << c << endl;
expected results is the numbers i typed in but printed out in numerical order.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Enter three whole numbers" << endl; int a,b,c; cin >> a >> b >> c;
if(a<b<c) {
cout << a << "," << b << "," << c << endl;
}
if(b<a<c) {
cout << b << "," << a << "," << c << endl;
}
if(c<a<b) {
cout << c << "," << a << "," << b << endl;
}
return 0;
}
#include <iostream>
#include <utility>
int main() {
std::cout << "Enter three whole numbers" << std::endl; int a,b,c; std::cin >> a >> b >> c;
if (a > b) std::swap(a, b); // a < b ? c
if (b > c) std::swap(b, c); // a ? b < c and a < c
if (a > b) std::swap(a, b); // a < b < c
std::cout << a << "," << b << "," << c << std::endl;
return 0;
}

meet error while trying to read int number from input file

I am new to coding C++. I meet issue while trying to read 2 int number from input file, calculate them and then export to output. System showing issue at line 14,18 and 21 of the loop. Please give me advice on this. Thanks all!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
file.open("input.txt");
string word;
word.clear();
int a, b;
int count = 0;
while(file>>word)
{
count++;
if (count % 2 == 1){
a = stoi(word);
}
else {
b = stoi(word);
}
}
int TOTAL = a + b;
int Difference = a - b;
int Multiply = a * b;
int Division = a / b;
int MUD = a % b;
ofstream out("output.txt");
out << "Input Values: " << a << " " << b << endl;
out << "Sum of two numbers: " << TOTAL << endl;
out << "Difference of two numbers: " << Difference << endl;
out << "Multiply of two numbers: " << Multiply << endl;
out << "Division of two numbers: " << Division << endl;
out << "Modular division of two numbers: " << MUD << endl;
cout << "Calculation written in file" << endl;
out.close();
return 0;
}
#include <string> because you are using string functions that's the problem I think

Error: control reaches end of non-void function? [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 6 months ago.
This is my assignment : " Write a function that finds the larger of two integers input (in the main program) but allows you to change the value of the integers in the function using pass by reference." I been trying to find out whats wrong with my code but I cant find out what it is. Someone please help me !! this assignment is due tonight!!
Here is my code as of right now :
#include <iostream>
using namespace std;
int change(int& x, int& y)
{
int temp=0;
cout << "Function(before change): " << x << " " << y << endl;
temp = x;
x = y;
y = temp;
cout << "Function(after change): " << x << " " << y << endl;
}
int main()
{
int x,y;
cout << " Please Enter your first number: ";
cin >> x;
cout << " Please Enter your second number: ";
cin >> y;
if (x > y)
cout << x << " is greater than " << y << endl;
if (x < y)
cout << y << " is greater than " << x << endl;
if (x == y)
cout << x << " is equal to " <<y << endl;
cout << "Main (before change): " << x << " " << y << endl;
change(x, y);
cout << "Main (after change): " << x << " " << y << endl;
system("Pause");
return 0;
}
change is declared as returning an int, but it never returns anything. It doesn't look like your function should return anything, so just declare it as void:
void change(int& x, int& y)

Printing multiple outputs on same line

there. I'm self learning C++ out of "C++ without fear". There is an exercise dealing with the GCD of 2 numbers that asks to print "GCD(a,b) =>" at each step in the proceedure. I was able to get this working:
int gcd (int a, int b);
int main() {
int i,j;
cout << "Enter the first integer" << endl;
cin >> i;
cout << "Enter the second integer" << endl;
cin >> j;
int k = gcd(i,j);
cout << "The GCD is " << k << endl;
system("PAUSE");
return 0;
}
int gcd (int a, int b){
if(b==0){
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " <<endl;
return a;
}
else {
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
return gcd(b,a%b);
}
}
I was just wondering if there is a nicer way to go about printing each step of finding the GCD. That is, is there a "nicer" way to write this part of the code:
cout << "GCF(" << a;
cout << "," << b;
cout << ") => " << endl;
? Thanks in advance.
You can do something like:
cout << "GCF(" << a << ',' << b << ") =>" << endl;
It is not C++ but you could use the C way of printing it which in my opinion looks better in this situation because there are far fewer stream operators, << in the way.
#include <cstdio>
printf("GCF(%d, %d) =>\n", a, b);
But this is a C way of doing things... You could use something like boost::format as is mentioned in this SO answer.
try this one
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
cout << 6+2 <<"\n" << 6-2;
}

C++ - program has stopped working

The code that I posted below is supposed to work in recursion (the Sort() function) even up to 1kk times. The problem is: when the Sort() function gets into loop number 43385 the console stops working and alerts: "The program has stopped working". Is it a problem with memory? If yes, where is the bad part of the code? Greetings.
#include <iostream>
#include <string>
using namespace std;
string a, b;
int n=0,i=0,counter=0;
int Sort(int i)
{
int x=0,y=0,tmp0=0;
char tmp1;
for(x=i;x<n;x++) {
if(a[x]==b[i]){
tmp0=x;
tmp1=a[x];
break;
}
else
continue;
}
for(y=tmp0;y>=i;y--)
y==i ? a[i]=tmp1 : a[y]=a[y-1];
counter+=tmp0-i;
if(i==n-1)
return counter;
else
Sort(i+1);
}
int main()
{
cin >> n >> a >> b;
Sort(0);
return 0;
}
Perhaps a call stack overflow because of too deep recursion?
To add to iltal's comment, you may want to print out information on strings a, b: a.size(), a.length(), a.capacity(), a.max_size()
I'm not sure what this code is trying to do. Here's a revision, with some print statements added, along with a random string generator.
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
string a, b;
int n=0,i=0,counter=0;
int Sort(int i)
{
int x=0,y=0,tmp0=0;
char tmp1;
for(x=i;x<n;x++) {
if(a[x]==b[i]){
tmp0=x;
tmp1=a[x];
cout << "x = " << x << " set tmp0 to " << tmp0 << " and tmp1 to " << tmp1 << endl;
break;
}
else
continue;
}
for(y=tmp0;y>=i;y--)
y==i ? a[i]=tmp1 : a[y]=a[y-1];
counter+=tmp0-i;
cout << " endof sort: a is " << a << endl;
cout << " b is " << b << endl;
if(i==n-1) {
cout << "Returning counter " << counter << endl;
return counter;
} else {
cout << "Running sort(" << i << " + 1)" << endl;
Sort(i+1);
}
}
string randomStrGen(int length) {
static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
string result;
result.resize(length);
for (int i = 0; i < length; i++)
result[i] = charset[rand() % charset.length()];
return result;
}
int main()
{
n = 50;
srand(time(NULL));
string a0, b0;
a0 = randomStrGen(n);
a = a0;
b0 = randomStrGen(n);
b = b0;
// cin >> n >> a >> b;
cout << "Max string size is " << a.max_size() << endl;
cout << "Calling sort" << endl
<< " n is " << n << endl
<< " a is " << a << endl
<< " b is " << b << endl;
Sort(0);
cout << " endof program: a inital: " << a0 << endl;
cout << " a final: " << a << endl;
cout << " b inital: " << b0 << endl;
cout << " b final: " << b << endl;
return 0;
}
counter is of type int but it has a lot of values summed in it which may be in all larger than int. maybe try int64?
You could hard code some test cases, like n = 20, a = "xyz...", b = "abc...", and add print statements to your sort function to track what is going on. Also, it may be helpful to add some comments to clarify what the purpose of the different loops are.