Returning zero from main breaks my program - c++

I just begin to learn C++, for the main method i do:
#include <iostream>
using namespace std;
int main ()
{
int d;
int n;
cout <<"Enter the denominator: " << endl;
cin >> d;
cout <<"Enter the numerator: " << endl;
cin >> n;
cout <<"The result of operation is: " << endl;
cout << (double)n/d << endl;
cout <<"Done";
return 0;
}
It doesn't produce output, but if I delete return 0. I will generate correct output. Shouldn't the main method in C++ always return an integer eventually?

Try cout.flush(); before return.
It forces the buffered data to be send to the output.

I went through your code and everything seems to be right. When I run it, it works fine. If you haven't solved it yet try to cut and past your code into a new project. I know that it sounds stupid but it should work.
I hope this will help you.

Related

C++ not waiting for an input

so I have been learning C++ and was working on a monkey see monkey do program and i managed to get the first input working but the second input it just skips straight over it, i have no clue why and any help would be apreciated.
#include <iostream>
#include <string>
using namespace std;
char monkey_1;
char monkey_2;
int msmd()
{
cout << "monkey one:"; cout << endl;
cin >> monkey_1;
system("cls");
cout << "monkey two:"; cout << endl;
cin.clear();
cin >> monkey_2;
cout << "waiting"; cout << endl;
if (monkey_1 == monkey_2)
{
cout << "both monkeys are happy."; cout << endl;
}
else
{
cout << "the monkeys are upest."; cout << endl;
}
return 0;
}
void main()
{
msmd();
}
Do you intent to only get a single character from the input as the monkeys are of type char? If not, change them to string, otherwise it will only assign a single character per cin.
If you want to input a sentence, cin also splits on spaces, so if you enter "something else", the first cin will assign something to monkey_1, and the second cin will automatically assign else to monkey_2.
To get around this you can use getLine(cin,monkey_x).
There are two point needs to be modified.
char monkey_1 and mokeny_2 should be declared as string for get more than 1 character.
void main need to be changed as int main.

Making an AI program for a project. Can't get it to recognise the user input

So I have to create an AI program than interacts with the user and responds based on the user input. I'm not very experienced, and this has already took hours lmao, I've looked online but I figured I'd actually post my code and try get some help/advice.
Basically the AI helps with maths, I have the program introducing itself and asking what it wants help with but when I enter Addition, Subtraction etc it just responds with numbers when it should respond with "Great, I'll help you with Addition!/(whatever user input)"
Screenshot of first running program: http://prntscr.com/elw7b4
Screenshot after entering what user needs help with: http://prntscr.com/elw7ky
(Obviously it's a bit all over the place at the moment, I did the calculator before anything else hence why it's giving additional results.
The calculator was working before entering the following code: (As you can see http:// prntscr.com /elwavs only two links cos haven't got more than 10 rep)
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
float inpsum;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
but entering the above code broke the calculator.
here is the full code:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
//user inputs what he needs help with/program output
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}
//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}
//division function
void Div()
{
float div1, div2;
cout << "Please enter two values you want divided" << endl;
cin >> div1;
cin >> div2;
cout << "The answer is: " << (div1 / div2) << endl;
}
//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}
int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();
return 0 ;
}
Basically - I've set the calculator up, and it was working. But upon trying to implement input and output between the user and the program I'm going wrong and have broken everything. Instead of the program saying "Great I'll help you with Addition", it says "Great, I'll help you with -134567432"
I'm not asking for anyone to do it for me, rather point me in the right direction so I can actually know what to do in the future.
Notice you define inpsum using float inpsum;, but what you are trying to store is string, or words. They are not compatible. You can learn something more about data types and strings in C++.
It might help to use an enum since you only have a handful of choices. You could do something like this:
enum class OPERATION : char {
Addition = 'A',
Subtraction = 'S',
Division = 'D',
Multiplication = 'M'
};
Then instead you cin to a string and have the following:
std::string input;
std::cin >> input;
switch(static_cast<OPERATION>(input[0])) {
case OPERATION::Addition:
Add();
break;
case OPERATION::Subtraction:
Subt();
break;
case OPERATION::Division:
Div();
break;
case OPERATION::Multiplication:
Mult();
break;
default:
std::cerr << "Invalid input" << std::endl;
exit(1);
}
Defining the enum will allow you to cast values to it which match its values. This allows you to safely do a switch with defined inputs that you expect to see as your program runs.

Code doesn't proceed beyond input stage

I've constructed a simple program that is supposed to take an amount in cents that the user inputs and outputs it in the form of dollars and cents, however, when I run it, it asks me for the amount, I input it, and the program doesn't proceed. I'm new to programming and would dearly appreciate any help I can get on the matter
#include <iostream>
using namespace std;
int main()
{
int numb;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
cin >> numb;
while (numb>=0);
{
dollars=int(numb/100);
cents=numb%100;
}
cout << dollars << "dollars" << cents << "cents"<<endl;
return 0;
}
The code below should work as you expected.
Read the comments I inserted into your code for more information.
Also, you may want to insert extra check in the loop for invalid input (i.e. non-digit characters) as that will cause the loop to enter an infinite loop.
EDIT: I've updated the code with the extra check to handle invalid non-numeric user input.
#include <iostream>
using namespace std;
int main()
{
int numb = 0;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
// I suspect your while loop here is to keep soliciting input
// if the input is not valid, so I've moved the "cin" into the loop.
// Don't use semi colon here after the while statement because
// in doing so, you're eliminating the body of the loop.
while (numb <= 0)
{
cin >> numb;
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<int>::max(), '\n');
cout << "Non numeric input, please try again." << endl;
continue;
}
cout << "numb: " << numb << endl;
dollars=int(numb/100);
cents=numb%100;
}
// I've inserted extra spaces around dollars and cents here
// to make the output more readable.
cout << dollars << " dollars " << cents << " cents"<<endl;
return 0;
}
This while loop doesn't make any sense. Remove it.
while (numb>=0); // remove this line and the '{' '}'
{
dollars=int(numb/100);
cents=numb%100;
}
Generally when using loops check that they will terminate. You are checking every iteration that numb>=0 but inside the loop numb never changes it's value. So the program executes this loop forever.
You have an infinite loop with your 'while' block, because numb is always positive (it doesn't change for any iteration).
Try removing the while loop to let the program proceed.
#include <iostream>
using namespace std;
int main()
{
int numb;
int dollars=0;
int cents=0;
cout << "Enter the amount" << endl;
cin >> numb;
dollars=int(numb/100);
cents=numb%100;
cout << dollars << "dollars" << cents << "cents" << endl;
return 0;
}
You can also have a look at other answers that give you ways to use a loop to repeatedly ask for (valid) user inputs.
you made an infinite loop
while (numb>=0);
{
dollars=int(numb/100);
cents=numb%100;
}
numb is let's say 3, so this always 3>=0 equals true therefore, you neverexit that loop

Cin and point to function chosen

I am not understanding how to make it so when i input my selection in testSelection for it to point to the function test. How do i go about doing this? Shouldn't it just go there?
#include <iostream>
using namespace std;
int test (int testSelection);
int main()
{
int testSelection;
cout << "Welcome to the pizza place!" << endl;
cout << "Choose 1 for pizza or 2 for drinks: ";
cin >> testSelection;
return 0;
}
int test (int testSelection)
{
if (testSelection== 1)
{
cout << "select your Pizza" << endl;
}
if (testSelection== 2)
{
cout << "Please select your drink" << endl;
}
else
cout << "test";
return 0;
}
You need to call the function...
cin >> testSelection;
test(testSelection);
Basically, you've written a function definition int test(int testSelection) {...code...} but, it's just dormant code until you invoke it by calling it.
I'm not sure what you are asking exactly. testSelection is an int, not a function which returns an int (which test is). Please elaborate on what you are trying to accomplish here if I'm off track, you never even call test. As far as I can tell, what you actually want is:
int test (int testSelection);
int main()
{
int testSelection;
cout << "Welcome to the pizza place!" << endl;
cout << "Choose 1 for pizza or 2 for drinks: ";
cin >> testSelection;
// you actually have to call the function...
test(testSelection);
return 0;
}
I didn't add any input validation (you should check that the cin actually grabbed a valid integer).

Program stops after cin

Hi there I am writing this program but I can't get it to even start to see if the other code has problems.
I have this:
int main()
{
int answer;
int test;
cout << "Please Enter the number to be tested: ";
cin >> test; //Gets number to be tested
cout << "here";
answer = factor(test);
cout << "The answer is:" << answer;
return 0;
}
now then. It will print out the first cout, and then it gets to the cin, takes the number but then won't do anything. Won't even print the second cout. Any ideas?
I"m pretty new and haven't really done much so any extra treating me like an idiot explanations are welcomed. :D Thanks.
Maybe there is something wrong with the factor function? An infinite loop?
Then cout << "here" << endl; (to flush the output) should at least print "here".
I guess << endl;is missing in your cout lines. That causes the output buffer not not be flushed and nothing to appear on the screen. Although that might be dependent on the platform you are running it on. It might work on some systems that flush the output buffer permanently.
int main()
{
int answer;
int test;
cout << "Please Enter the number to be tested: ";
cin >> test; //Gets number to be tested
cout << "here" << endl;
answer = factor(test);
cout << "The answer is:" << answer << endl;
return 0;
}
Looks like the program is waiting for the input at the terminal . Once you provide the input and then press "Enter" it will automatically consider the input and the next cout statement works fine ...
Check the below code segment ... ( nothing modified except the dummy implementation for factor, which is not the topic of discussion here )
enter code here
include
using namespace std;
int factor(int t) {
return t;
}
int main()
{
int answer;
int test;
cout << "Please Enter the number to be tested: ";
cin >> test; //Gets number to be tested
cout << "here";
answer = factor(test);
cout << "The answer is:" << answer;
return 0;
}
O/p is :
$ ./a.out
Please Enter the number to be tested: 1234
hereThe answer is:1234user#ubuntu:~$ ./a.out
Please Enter the number to be tested: 1234
hereThe answer is:1234$
I got the same results when I was hitting enter on the keypad right after my input. If I hit return then the program runs fine. I thought enter and return are the same thing?
This is how it work "here " your screen doesnot be static by putting system("pause") you can do it ,on the other hand i have just made the function defination dummi still happen anything check there]
#include<iostream>
using namespace std;
int factor(int x)
{
return x;
}
int main()
{
int answer;
int test;
cout << "Please Enter the number to be tested: ";
cin >> test; //Gets number to be tested
cout << "here";
answer = factor(test);
cout << "The answer is:" << answer;
**system("pause");**
}