My C++ program not working properly [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have to make a program in C++ that will read numbers and then arrange them in ascending order. The numbers can be infinite, so the program should read numbers until any particular value is entered to terminate the reading process. I have written below code but is not working and showing undesired output. I will be so thankful if someone will help me.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *p,*q,i=1,j,k,temp;
p=(int*)malloc(sizeof(int));
cin>>*p;
while((*p)!=-1) //read until -1 is entered
{
i++;
p=(int*)realloc(p,sizeof(int)*i);
q=p;
p=p+(i-1); //going to next address to read
cin>>*p;
}
p=q;
for(j=1;j<i;++j)
{
for(k=0;k<i-j-1;++k)
{
if((*(p+k))>(*(p+k+1)))
{
temp=*(p+k);
*(p+k)=*(p+k+1);
*(p+k+1)=temp;
}
}
}
cout<<"\n";
for(j=0;j<i-1;++j)
{
cout<<*(p+j)<<" ";
}
}

Expanding on my comment, here is what an actual C++ solution might look like:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers;
int number = -1;
std::cin >> number;
while (number != -1)
{
numbers.push_back(number);
number = -1;
std::cin >> number;
}
std::sort(numbers.begin(), numbers.end());
for (int x : numbers)
{
std::cout << x << ' ';
}
std::cout << '\n';
}

After
p=p+(i-1);
p is no longer a pointer that is valid to realloc.
Replace
p=p+(i-1);
cin>>*p;
with
cin >> p[i-1];
and get rid of q.
(You can use cin >> *(p + i - 1); if you insist on obfuscation.)
Your sorting routine also becomes much more readable if you replace the pointer arithmetic with indexing.

Related

Why does my c++ code keep printing a random number? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Hi I need help with this code. It keeps on printing "ur input:32765" and the number keeps changing. I read a question on stack overflow and it said it wasn't initialized, whatever that means. Can someone help with whats wrong?
#include <iostream>
using namespace std;
int main() {
int x;
cout << "ur input:";
cin >> x;
cout << "" << x;
return 0;
}
Write
if (cin >> x){
cout << "" << x;
} else {
cout << "bad input";
}
otherwise a read of an unintialised x could arise if the cin fails in C++03 or earlier. That can happen if there are not data on the stream that can be read into an int type.

How to convert data[i].int into a vairable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So I'm kind of new to c++ and I'm currently working with strings. and I want to input some amount and compare them to each other, but since i have them in data type in arrays it wont let me do the substrcution and I don't understand why
for (int i=0;i<N;i++)
{
cout << "Name"<< endl;
cin >> data[i].name;
cin >> data[i].all;
cin >> data[i].con;
}
exceed = data[i].con-data[i].all;
while (exceed > maxvalue){
maxindex = -1;
maxvalue = exceed;
if (maxvalue > 0){
cout << data[i].name;
}
Without knowing what type or struct or class you're using for your data member, or what error you're encountering it's hard to tell you what exactly is going on. You are also referencing i outside of your for loop so that may be your issue.
I've recreated a short program that seems to be doing what you're going for with a simple struct. Because the struct defines con and all as int types, they are converted on input, and i is no longer referenced outside of the for loop.
#include <iostream>
#include <string>
struct dataType {
std::string name;
int all;
int con;
};
int main() {
int N = 2;
int maxValue = 3;
dataType data[N];
for (int i = 0; i < N; ++i) {
std::cout << "Name" << std::endl;
std::cin >> data[i].name;
std::cin >> data[i].all;
std::cin >> data[i].con;
int exceed = data[i].con - data[i].all;
if (exceed > maxValue) {
std::cout << data[i].name << std::endl;
}
}
}
If you are using a struct or something where con and all are strings, there is a method in std::string stoi that can convert string types to int. Below is a short example.
int x;
std::string test = "4";
x = std::stoi(test);
std::cout << x << std::endl;
Note that an invalid argument in stoi throws an exception, but as a beginner you probably haven't learned about exception handling yet (but you should once you get the hang of things).
Hope that helps, cheers.

returning a std::list from a function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've been given an assignment to build the following function template "list primeFactors(unsigned long int n)". The function returns a list of integers of the prime factorization of a natural number. I've created a program that can prime factorization but I'm having issues using a list.
#include <list>
#include <numeric>
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
list<unsigned long int> primeFactors(unsigned long int n)
{
list<unsigned long int> list;
for (unsigned long int i=2; i <=n; i++)
{
while(n % i == 0)
{
n /= i;
//cout << i << " ";
list.push_back(i);
}
return list;
}
}
int main()
{
unsigned long int n;
list<unsigned long int> plist;
cout << "Enter num: " <<endl;
cin>>n;
plist = primeFactors(n);
for(list<unsigned long int>::iterator it=plist.begin(); it != plist.end(); ++it)
{
cout << ' ' << *it;
cout << '\n ';
}
return 0;
}
My program is no longer returning the correct numbers of the factorization and I'm unsure what the issue is.
Any help is appreciated
This likely has nothing to do with returning the list. The problem is you always return it before finishing your iterations:
for (unsigned long int i=2; i <=n; i++)
{
while(n % i == 0)
{
n /= i;
//cout << i << " ";
list.push_back(i);
}
return list; // I think you meant to put this outside the for loop
}
// Probably here is better for the return.
Try using a debugger next time, you will see this issue much more quickly then posting here.
Just return the list outside the for loop
Line return list; is too early. Move it beyond the curly brace.

an error i can't seem to find [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am a beginner and still learning C++ I was playing with vectors to see what will happen.whenever I start it gives me a debug error.
here is the code
int main()
{
vector<int> num;
int i = 0;
while (i != 1000)
{
++i;
num.push_back(i);
cout <<num[i]<<"\t"<< sqrt(num[i]) << "\n";
}
}
Problem lies in the order of operations inside while() loop:
while (i != 1000)
{
++i;
num.push_back(i);
cout <<num[i]<<"\t"<< sqrt(num[i]) << "\n";
}
i starts from 0. In each iteration, you push_back an element and then print it using counter i - after its incrementation. So, num[i] refers to a non-yet-existing element.
Change your code to:
while (i < 1000)
{
num.push_back(i + 1);
cout <<num[i]<<"\t"<< sqrt(num[i]) << "\n";
++i;
}
The index of a vector starts from zero. In your code you were always accessing 1 index ahead of what you were updating(or pushing).
When I incremented i after the two statements it worked fine.
I dont know how but it worked fine.
Thanks everyone.
int main()
{
vector<int> num;
int i = 0;
while (i != 1000)
{
num.push_back(i);
cout <<num[i]<<"\t"<< sqrt(num[i]) << "\n";
++i;
}
}
Make sure that you are using
#include<vector>
#include<iostream>
using namespace std;

C++ Substracting adjacent elements in a vector [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a code to subtract the adjacent elements of a vector and enter the answer into a new vector. However, my code isn't working. What exactly is wrong with it?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int length;
vector<int>values;
vector<int>values2;
cout << "Enter the length of the vector";
cin >> length;
values[0]=1; values[1]=2; values[2]=3; values[3]=4; values[4]=5;
for(int i=0; i<length; i++)
{
cout<<"Enter the " << i <<"th element of the vector";
cin >> values[i];
}
for (int i=0; i<length-1; i++)
{
values2[i]=values[i+1]-values[0];
}
return 0;
}
You need to size the vectors accordingly before accessing elements. You can do that on construction, or using resize.
vector<int>values(5/*pre-size for 5 elements*/); and similar for values2 would fix your problem.
Currently your program behaviour is undefined.
If you want to subtract adjacent elements, then shouldn't you have values2[i]=values[i+1]-values[i];?
The line of code:
values2[i]=values[i+1]-values[0];
will take the looked-at element away from the first element each time.
Did you mean:
values2[i]=values[i+1]-values[i];
?