Id returned 1 exit status. C++ - c++

I'm very new to C++, so I've probably made some really stupid mistakes. But I've looked online to solutions for this error, and I tried all I can think of.
I'm trying to have my whole program in one function, because it's going to be merged with other programs. The id error is the only error I have so far.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
using namespace std;
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0);
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0);
void BookingSystem(int time, int k);
int time;
int k = 0;
int main()
{
BookingSystem(time, k);
{
while (k==0)
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
scanf("You have selected to book your meal for %d", &time);
if (time!=7||time!=9)
{
printf("Sorry, that was an incorrect time");
time = 0;
}
}
return 0;
}
system ("pause");
return 0;
}
Any help is much appreciated, I've spent a long time trying to fix this on my own with no luck.
Thank you!

You make many mistakes in your program:
1) You mix C & C++ syntax.
using namespace std, is C++
2) You include useless header files
3) You declare variables that I don't understand what for
Why do you need this?
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0);
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0);
You don't use the variables above anywhere !!!!
4) You try to write a function inside main (this is not Pascal).
So, if I understand what you want, look at this:
#include <stdio.h>
void BookingSystem() // this is the function who does all job
{
int time = 0;
while ((time != 7) && (time != 9))
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
printf("\nYou have selected to book your meal for ");
scanf("%d", &time); // read the time
scanf("%*[^\n]"); // consume all caracters until the newline
scanf("%*c"); // consume the newline
if (time == 7)
{
printf("You have selected to book your slot at 7PM\n");
}
else if (time == 9)
{
printf("You have selected to book your slot at 9PM\n");
}
else
{
printf("You have selected an incorrect time, please try again\n");
}
} // end while
} // end function
int main(int argc, char *argv[])
{
BookingSystem(); // this is the calling to your function
return 0;
}
If I understand what you want, this works fine.

you can not to Define Function in to the main ..
you must to do like this...
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
using namespace std;
int ninebooking(int , int , int ); //just say data type int or ....
int sevenbooking(int , int , int );
void BookingSystem(int , int);
int main() //and in to main just call he...
{
int time;
int k = 0;
BookingSystem(time, k);//call ....and send parameter time and k
system ("PAUSE");
return 0;
}
void BookingSystem(int time,int k)
{
while (k==0)
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
scanf("You have selected to book your meal for %d", &time);
if (time!=7||time!=9)
{
printf("Sorry, that was an incorrect time");
time = 0;
}
}
// return 0;//if this function is void he can not to return any thing!!!!
}
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0)
{
//......
}
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0)
{
//........
}

Related

Count the occurence of a particular digit in a range of number

I was trying to count the number of digit 2 in an range of number (say 2-22, the answer would be 6: 2,12,20,21,22 as 22 contribute twice). This is the code that I came up with, yet it fails to run after value input. Any ideas?
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int lowerbound,upperbound,sum=0;
int nofNum(int num);
scanf("%d %d",&lowerbound,&upperbound);
for (int i=lowerbound;i<=upperbound;++i){
sum+=nofNum(i);
}
printf("%d",sum);
return 0;
}
int nofNum(int num){
int count=0;
while(num!=0){
if (num%10==2){
count++;
num/=10;
}
}
return count;
}
you are using c not c++. Your mistake is that nofNum wasn't declared before you used it. It has to be declared before the line you use it.
int nofNum(int num);
would declare it.
You will still need to implement it, which you allready did.
alternatively you can move the implementation st. it is above main, where you used it.
EDIT: i just saw you declared it inside main, which is uncommon at best. you really should not do that.
EDIT2:
you messed up that if statement in numOf
int nofNum(int num){
int count=0;
while(num!=0){
if (num%10==2){
count++;
}
num/=10; // may not be inside if, since num would not be adjusted
// if the last digit isnt a 2
}
return count;
}
EDIT3:
you can use input and output streams in c++ to replace scanf and printf:
scanf("%d %d",&lowerbound,&upperbound);
becomes
std::cin >> lowerbound >> upperbound;
and
printf("%d",sum);
becomes
std::cout << sum << std::endl;
Edit4:
suggested form:
// declarations - this is what would belong to the *.h file later on.
int nofNum(int num);
followed by
int nofNum(int num) { /*implementation*/ }
int main(int /*argc*/, char* /*argv*/[]) { /*implementation*/ }
or
// this is valid because we allready heard of nofNum through declaration
int main(int /*argc*/, char* /*argv*/[]) { /*implementation*/ }
int nofNum(int num) { /*implementation*/ }
the upper form doesn't require the declarations because each function allready is implemented before you use them, therefore the compiler allready knows what nofNum is supposed to be.

Hello, This simple C++ script pops out an error at line 11. Does anybody know how to fix it?

Description
This code is intended to find a spanner index (i) when dealing with
Splines/NURBS basis functions based on a knot vector (U), a choosen knot (u), the degree of the desired curve (p) and the number of basis funcions (n). The algorithm was taken from the NURBS Book by Piegl and Tiller. The error, I guess, is in the way I declared variable U. Thaks in advanced!
code
# include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(n,p,u,U) /* an error in this line */
{
if (u==U[n+1]) return (n);
low=p; high=n+1;
mid=(low+high)/2
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2
}
return (mid);
}
You have forgotten some semicolons and types!
here is the correct code:
#include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(int n, int p, int u, int U[])
{
if (u==U[n+1]) return (n);
int low=p, high=n+1;
int mid=(low+high)/2;
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2;
}
return (mid);
}
int main() {
cout << FindSpan(n, p, u, U);
return 0;
}

Binary Search with few modification

While I am trying to compile the code with few modification in binary search recursive function. The program is acting weird. Some time it gives the correct value and some time it goes to infinite loop. Please explain what went wrong with the code. I am using DEV C++ as an IDE.
CODE:
#include<iostream>
#include<sstream>
using namespace std;
//function to compare the two integers
int compare(int low, int high)
{
if (low==high)
return 0;
if (low<high)
return 1;
else
return -1;
}
//Function for binary search using recursion
int *BinarySearch(int *Arr,int Val,int start,int end)
{
int localstart=start;
int localend=end;
int mid=(start+end)/3;
cout<<"MID:"<<mid;
int comp= compare(Val,Arr[mid]);
if(comp==0)
return &(Arr[mid]);
else if (comp>0)
return BinarySearch(Arr,Val,localstart,mid-1);
else
return BinarySearch(Arr,Val,mid+1,localend);
return NULL;
}
main()
{
int *arr;
arr= new int [256];
string str;
getline(cin,str);
stringstream ss;
ss<<str;
int index=0;
while(ss>>arr[index])
{index++;}
//cout<<arr[index-1];
cout<<"Enter Value:";
int value;
cin>>value;
int *final;
final=BinarySearch(arr,value,0,index-1);
if(final!=NULL)
cout<<"Final:"<<*final;
else
cout<<"Not Found";
getchar();
getchar();
return 0;
}
Two ideas:
What should BinarySearch do if Val is not in the array? Trace out what your code does in this case.
(start+end)/3 probably isn't the middle of the current range.

trying to create a dice game using function c++

i am trying to have a function determine the result
a. If the numbers add up to 5, 7, or 12, then the player wins, and the function should return an indication of this (use some integer to represent a win).
b. If the numbers add up to 2, 4, or 11, then the player loses, and the function should return an indication of this (once again, use an integer).
c. If the numbers add up to anything else, then the game is a draw and the function should say this (by way of an integer).
question, do i need a different func for winner, loser, and draw?
and what how can i return a integer to main to let main know that if we have a winner a loser a draw.
just learning to program any help would be greatly appreciated
//function
int outcome(int, int)
{
int die1;
int die2;
int winner;
int loser;
int draw;
if (die1&&die2==5||7||12)
return 99;
if (die1&&die2==2||4||11)
return loser;
else
return draw;
}
// func to get a random number
int rollDice()
{
int roll;
roll = (rand()%6)+1;
return roll;
}
the main func
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
double die1=0;
double die2=0;
int winner=0; //counter for winners
int loser=0; //counter for losers
int draw=0; //counter for draw
//func to determine the outcome
int outcome(int, int);
//func for random numbers
int rollDice();
int outcome(int, int)
if (return==99)
cout <<"winner";
Your code has several syntax errors. If I want to, say, make a function to add two integer numbers, I'd do something like this:
int add(int a, int b) //VARIABLES MUST CARRY A NAME!
{
return a+b;
}
If you want to work with conditions, do this:
if(a==5 && b==6 || a==6 && b==7) //Just as an example
Your fixed condition would be this:
if (die1+die2==5 || die1+die2==7 || die1+die2==12)
Also, study variable scope. Let's say I have the following:
int main()
{
int myVar = 1;
}
int anotherFunction()
{
println("%d", myVar); //This will cause an error, because myVar doesn't exist here, it only exists in main()
}
These are the most notable errors I can see in your code.

bsort implementation from programming pearls

This question is a follow-up to a question I asked earlier on Stack Overflow:
bsort example from programming pearls
I want to show you my work and have some questions about it.
#include<string.h>
#include<iostream>
using namespace std;
void swap(char *x[],int i,int j)
{
char *t=x[i];
x[i]=x[j];
x[j]=t;
}
int get_bit(char *s,int depth)
{
int required_bit=13;
int bit=required_bit&0x7;
int byte=required_bit>>3;
int val=(s[byte]>>bit)&0x1;
return val;
}
void bsort(char *x[],int l,int u,int depth)
{
if(l>=u) return ;
for(int i=l;i<=u;i++){
if(strlen(x[i])<depth){
swap(x,i,l++);
}
}
int m=l;
for(int i=l;i<u;i++)
{
if(get_bit(x[i],depth)==0)
{
swap(x,i,m++);
}
}
bsort(x,l,m-1,depth+1);
bsort(x,m,u,depth+1);
}
int main()
{
char *x[6]={"car","bus","snow","earth","dog","mouse"};
bsort(x,0,5,1);
for(int i=0;i<6;i++)
cout<<x[i]<<" "<<endl;
return 0;
}
The code above works, compiles and shows me a result. But I am interested to know whether it sorts lexicographically or otherwise? The result is this:
car
bus
dog
snow
earth
mouse
First b is before c, so are car and bus on the right place? Please tell me what is wrong with my code.