C++ basic coding issue - c++

#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
I want to see the output is about the integers inclusive between a and b, but after entering two numbers, it shows no output..

#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b>>endl;
for(int i=a;i<=b;++i)
cout<<i<<endl;
return 0;
}
First you can't use endl in cin
Second you wrote ++i inside your for loop which will increase value of by i means value of will become 1 from 0.
Therefore the condition will never be true as value of b is 0.
THE CORRECT WAY
#include <iostream>
using namespace std;
int main() {
int a=0,b=0;
cin>>a>>b;
for(int i=a;i<=b;i++)
cout<<i<<endl;
return 0;
}

#include <iostream>
using namespace std;
int main() {
int a = 0, b = 0;
cin >> a;
cin >> b;
for (int i = a; i <= b; i++)
cout << i << endl;
return 0;
}
EDIT: I removed something as it wasn't true :P Silly me.
Also 'endl' doesn't work with cin :)

The code is wrong because you have already got a and b equal to 0,and after that you are taking a and b as inputs.
If you wanna take them as inputs you should write int a,b. NOT int a=0,b=0

Related

how to find summation of all elements of array c++

I'm new to c++ but long story short , i want to write a c++ program that will accept input from user through an array and it will sum each array element input
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main() {
int array[100];
int sum;
for(int i=0; i<100; i++){
cout<<"Insert element "<<i<<": ";
cin>>array[i];
sum = array[i]+ //summ with the next array input;
cout<<sum;
}
return 0;
}
means that if i enter any integer the program should be able to give the summation of the inputs in sequence from the first input to the last input
C++ standard library already has a function to do this which is std::accumulate:
#include <iostream>
#include <numeric>
int main() {
int array[5] = {1,2,3,4,5};
int total = std::accumulate(std::begin(array), std::end(array), 0);
return 0;
}
If you plan to use not a full array you should use std::begin(array), std::begin(array) + amount as ranges.
initialize your sum var to 0 initially and write sum+=array[i] instead of what you have written, there is also a limitation in this program as value of sum after all user input should be <=10^9 as int datatype store no. approximately upto 10^9 so take note of this fact also. Write cout<<sum<<endl; instead to be able to distinguish till previous input sum to the new input sum.
Hope this will help.
You can use a do while loop:
#include<iostream>
#include<string>
#include<math.h>
int main()
{
int array[100];
int sum=0;
int i=0;
std::cout<<"Insert element"<<" "<<i<<": ";
std::cin>>array[i];
do
{
sum=sum+array[i];
std::cout<<sum<<std::endl;
i++;
std::cout<<"Insert element"<<" "<<i<<": ";
}while(std::cin>>array[i]);
return 0;
}
If all you want is the sum then just compute the sum. No need to store anything:
#include <iostream>
#include <string>
#include <math.h>
int main() {
int sum = 0;
for(int i=0; i<100; i++){
std::cout << "Insert element " << i << ": ";
int t;
std::cin >> t;
sum += t
std::cout << sum;
}
}

Take the sum of two integers and add another integer to the sum continuously

I would like to make a C++ program that takes the sum of two integers and adds another integer to the sum continuously. Please help, I'm a beginner. My code looks like this, except it doesn't update the next sum:
#include <iostream>
using namespace std;
int main() {
int x;
while (cin>>x){
int y=100;
int sum;
sum = x+y;
cout<<sum<<endl;
}
return 0;
}
Your sum variable is declared inside the loop body, so it will be wiped out on each iteration. You need to move that variable outside of the loop, eg:
#include <iostream>
using namespace std;
int main() {
int sum = 100, x;
while (cin >> x){
sum += x;
cout << sum << endl;
}
return 0;
}

Code with pointers gives out 1 no matter what I tried c++

So I'm doing a code which sums up all elements of field but I have to use pointers :( No matter what I tried I get 1 as output. Tried same code with multiplication but still nothing... Code:
#include <iostream>
#include <cmath>
using namespace std;
float vrat=0;
int suma (int pok2, int vel22){
for(int z=0; z<vel22; z++){
vrat+=pok2;
}
return vrat;
}
int main()
{
/// 2. zadatak
int vel2;
int *vel22=&vel2;
cout<<"Unesi broj elemenata koje hoces upisati"<<endl;
cin>>vel2;
int polje2[vel2];
cout<<"Kreni unosit elemente "<<endl;
for(int z=0; z<vel2; z++){
cin>>polje2[z];
}
int *pok2=&polje2[vel2];
suma(*pok2, *vel22);
cout<<"Suma elemenata je "<<suma<<endl;
return 0;
}
Thank you!
I think you meant to do something like this
#include <iostream>
#include <cmath>
using namespace std;
float vrat=0;
int suma (int *pok2, int vel22){
for(int z=0; z<vel22; z++, pok2++){
vrat+=*pok2;
}
return vrat;
}
int main()
{
/// 2. zadatak
int vel2;
int *vel22=&vel2;
cout<<"Unesi broj elemenata koje hoces upisati"<<endl;
cin>>vel2;
int polje2[vel2];
cout<<"Kreni unosit elemente "<<endl;
for(int z=0; z<vel2; z++){
cin>>polje2[z];
}
int *pok2= polje2;
suma(pok2, *vel22);
cout<<"Suma elemenata je "<< vrat << endl;
return 0;
}
this line
int *pok2=&polje2[vel2];
attempts to access memory outside of the bounds of polje2. Remember arrays are zero-indexed. Let me know if this isn't what you were intending to do.
Ah! It looks like at the very end you're printing the function suma rather than the result. Try this instead:
int result = suma(*pok2, *vel22);
cout<<"Suma elemenata je "<< result <<endl;
I'm not sure why printing a function results in "1", but printing the actual result should work here.

why does it shows error even though it seems to be quite appropriate?

#include <iostream>
using namespace std;
int factors(int);
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
factors(n);
return 0;
}
factors(int n){
//To find out factors
int i, arrA[5];
arrA[0]=1;
cout<<"Factors: ";
for(int i=1;i<=n;i++){
for(int j=2;j<=n;j++){
if(n%j==0){
arrA[i]=j;
}
else if(n%j!=0){
i--;
}
}
}
for(int z=0;z < (sizeof(arrA)/sizeof(arrA[0]));z++){
cout<<arrA[z]<<" ";
}
}
This is a c++ code and for finding the factors of a given number.
But the problem is it's not producing the desired output.Problem is with the processing part(I think). It shows only "factors: " and continues to process and gives an error stating - "Untitled.exe has stopped working". Please fix the error and provide an elaborate explanation.
Clearly the problem comes from the for(int i=1;i<=n;i++){ that contains i-- which is a risk for infinite loop.
Since you tagged c++, do it with c++ this avoids a static array (arrA[5] is a good thing if you are sure there is only 5 factors but it depends on your n)
Instead use a std::list
#include <iostream>
#include <list>
using namespace std;
void factors(int n, std::list<int> & factors);
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
std::list<int> fact;
factors(n, fact);
cout<<"Factors: ";
std::list<int>::iterator it=fact.begin();
for(;it!=fact.end();it++)
{
std::cout << *it<<", ";
}
return 0;
}
void factors(int n, std::list<int> & factors)
{
for(int j=2;j<=n;j++){
if(n%j==0){
factors.push_back(j);
}
}
}

c++ Scrabble game using arrays

I have to make a scrabble scoring game using 2 arrays. The first array holds the user inputted word and the second array holds the value of each letter. Lastly a function is needed to calculate the score. Im having trouble assigning the user values to the second array to get the score and getting the right code for the function.
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
int scoreCalculator(int total);
char userWord;
int points;
int total=0;
int main()
{
char userWord[11];
for (int i=0; i<11; i++)
{
userWord[i]='\0';
}
cout<<"Enter your word less than 10 letters: "<<endl;
cin>>userWord;
cout<<"Here is the word you inputted: "<<userWord<<endl;
int scrabblePoints[26]={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
for(int j=0; j<26; j++)
{
userWord[i]
}
cout<<"Here is your score: "<<scoreCalculator(total)<<endl;
}
int scoreCalculator(int total)
{
total+=scrabblePoints[j];
}
This is what I have so far and where im stuck at
#include <iostream>
int main()
{
std::string input;
std::cin>>input;
// fill this with scrable values
int scrable_values[26] = {1,3,3,2,1,4,2,4,1,9,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int ans = 0;
for(int i=0;i<input.size();i++)
{
ans += scrable_values[input[i]-97];
}
return ans;
}