#include<iostream>
using namespace std;
int main()
{
int num1,num2,result;
cout << "enter num 1"<<endl;
cin >> num1;
cout << "enter num 2"<<endl;
cin >> num2;
result = num1 + num2;
cout << num1 <<"+" << num2 << " = "<< result;
return 0
}
this is the code I am unable to enter in output for adding the numbers
I wrote c++ code of adding two nums when output is given to enter 1st no then I am unable to write the no how can I write that output
The last sentence your code :"return 0",didn't write ";".
Related
I am attempting use ifstream to extract two numbers from a file in argv[1], named "inputFile", and the extracting operator seems to be extracting the bits of code rather than the numbers needed.
inputFile.txt was put into the command line operator by right clicking the project, going to properties -> debugging -> command arguments -> typing inputFile.txt into command arguments in visual studio 2017.
The file inputFile.txt is as below:
1 2
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
//Test opening file
cout << "Input file: " << argv[1] << endl;
ifstream in(argv[1]);
if (!in)
{
cerr << "Unable to open " << argv[1] << " for input";
return 1;
}
//extract numbers
int num1;
int num2;
in >> num1 >> num2;
cout << num1 << endl << num2 << endl;
in.close();
return 0;
}
I expect the int num1 to hold 1, and the int num2 to hold 2, but instead each variable holds the number -858993460.
Are you sure the file you are reading has got the data you expect? This code works fine for me, compiling with Visual Studio 2005. But, If I change the contents of the file, writing not numbers (for example if I write: a b), num1 and num2 ends with -858993460.
I've try this case but I got stuck in a trouble. I just want to to determine length of a string from user input or from console. But it doesn't work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int N;
string S;
char vocal;
vocal = {'a'||'i'||'u'||'e'||'o'};
cout << "Length of Spaghetti Name: ";
cin >> N;
fflush(stdin);
cout << "Spaghetti Name: ";
cin >> S;
if (S[0] != vocal){
S.erase(0,1);
} else if (S[N] != vocal){
S.erase(N,1);
}
cout << S << endl;
cin.get();
return 0;
}
expected input
9
Carbonara
expected output
arbonara
This program will delete non vocal character in spaghetti names.
I am trying to make a random number generator guessing game, which will allow the user to pick a range. After the range is picked the user will try to guess the number until they get it right. I am new to c++ and do not understand why two cout outputs on screen. I am new to c++ and cant figure out why. I am using visual studio 17.
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
int userNum;
char choice;
int numattempts = 0;
int randNumber;
srand(time(0));
cout << "Welcome to the number guessing game\n" << "Please choose what range you want to guess from\n" << " '1' = 0-20\n '2'= 0-50\n '3'= 0-100\n ";
cin >> choice;
if (choice == '1')
{
randNumber = 0 + rand() % 20;
cout << "Enter a number between 0 and 20\n";
cin >> userNum;
cout << randNumber;
do {
if (userNum < randNumber)
{
cout << "Number is Higher\n";
cin >> userNum;
}
else if (userNum > randNumber)
{
cout << "Number is Lower\n";
cin >> userNum;
}
else (userNum == randNumber);
{
cout << "Congrats You got it!\n";
}
} while (userNum != randNumber);
}
you miss another if in the last checking and should not put semicolon there.
try to debug and you'll find that it should be like this
do {
if (userNum < randNumber)
{
cout << "Number is Higher\n";
cin >> userNum;
}
else if (userNum > randNumber)
{
cout << "Number is Lower\n";
cin >> userNum;
}
else if(userNum == randNumber)
{
cout << "Congrats You got it!\n";
}
} while (userNum != randNumber);
I have a user input of this kind:
A+B*C;
A*(B+c);
C+F.
Now I want to parse this input and get all sub-strings until the semi-colons and stop scanning when I run into a period symbol.
What is the simplest way to achieve this ?
Also, I have freedom to take input from a file or from the console. Which one would be the easiest to implement ?
I have a method to read from the console as such :
cout << "Enter input: " << endl;
char delimiter = '.';
string inputStr;
getline(cin,inputStr,delimiter);
cout << inputStr;
But if I enter the above mentioned sample input I read only until before period symbol is recieved. So while finding sub strings, what stopping criteria or flag should I take ?
TIA
EDIT:1
Code so far:
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout << "Enter input: " << endl;
char delimiter = '.';
string inputStr;
getline(cin,inputStr,delimiter);
cout << inputStr;
string deli = ';';
size_t pos = 0;
string token;
while ((pos = inputStr.find(deli)) != std::string::npos) {
token = inputStr.substr(0, pos);
std::cout << token << std::endl;
inputStr.erase(0, pos + deli.length());
}
std::cout << inputStr << std::endl;
};
ANSWER:
I was wrongly initializing the string deli. I had to do this :
string deli = ";". Instead of single-quotes, I had to use double-quotes, because it a string and not a character! Final working solution here: https://repl.it/EPyC/2
Use getline with delim ; for all lines. Check for dot(.) for last line.
string line;
while(getline(cin, line, ';')){
if(line.back() == '.')
line.pop_back();
cout << line <<endl;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main () // print to console: 3.0*5.0=15.00
{
double a;
double b;
a =(3.0);
b =(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "------" << endl;
cout << fixed << setprecision (2) << a*b << "\n" << endl;
return 0;
}
int calculate () // print to console: (7.1*8.3)-2.2=56.73
{
double a;
double b;
double c;
a = (7.1);
b = (8.3);
c = (2.2);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << b << "\n" << endl;
cout << "- " << c << "\n" << endl;
cout << "------" << endl;
cout << setprecision(2) << (a*b)-c << "\n" << endl;
}
int calculation () // print to console: 3.2/(6.1*5.0)=0.10
{
double a;
double b;
double c;
a=(3.2);
b=(6.1);
c=(5.0);
cout << " " << fixed << setprecision (1) << a << "\n" << endl;
cout << b << "*" << c << endl; //how can I use variables instead of using quotes?
cout << "------" << endl;
cout << setprecision(2) << a/(b*c) << "\n" << endl;
system("PAUSE");
return 0;
}
What does this output error mean? How do I fix it? someone please explain this to me. Am I suppose to add: int calculate(int a, int b, int c)?
Output:
(32): error C4716: 'calculate' : must return a value
You've declared your function as one that returns an int value but there's no return statement. Try changing the declaration to void calculate() if you don't need to return a value from it.
The calculate function needs to return a value.
You need to add something like this at the end of calculate:
return (a*b)-c;
It's only because your calculate function is supposed to return an int
and there's no return in your function.
if you don't want to return anything, you can put void calculate() instead