Console application crashes for unknown reason - c

I have my piece of code which is shown below
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a;
printf("Please select a choice \n1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist");
scanf("%d,&a");
if(a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
On running the code, it displays the menu and if i input 1, it takes a few seconds before it crashes and displays the info
document1.exe has stopped working
How can I debug this problem. I am using dev c++ 4.9.9.2

Your scanf statement is wrong. You are not passing argument (pointer) to it.
Change
scanf("%d,&a");
to
scanf("%d",&a);

For a C solution - I am not certain
aside from previously mentioned
error in scanf formatting (which your compiler should have warned about),
which would have put the result.. well, god knows where on stack…
I'm not certain, but I suspect the compiled code would take the next location in ram as an address, and write there.
Borked stack == really, really, really hard to debug errors.
Since you are using a c++ compiler, if you can use c++, you may wish to consider evaluation of using some of the STL here.
I assume you will be terminating input with a CR
int main (int argv, char** argv]
{
int a;
std::string inputString;
std::cout <<"Please select a choice \n""1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist";
std::getline(std::cin,inputString);
std::stringstream inputStream(inputString);
inputStream >> a; // could also have been parsed with std::stol, or strtol. - my preference due to error checking - what if your user entered 'byte me'?
if (a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}

Related

C - a certain function in my program wont work with either returning an integer nor using a pointer

This is my first time posting of this forum and I'm doing is just because of this problem. I've been working on a program for a while(just for fun) and to make things simple for my self I used loads of global variables, but now I've been trying to make the individual functions more independent and flexible. A certain function is giving a a lot of issues for some reason.
int which_move(int ac,int bc,int cc){
int illcheck;
int ill_done;
int ill_pos;
int true_move;
true_move=3;
ill_done=-1;
for(u=6;u>=0;u--){
ill_pos=ert-1;
illcheck=0;
for(y=0;y<ill_len[u];y++){
if(buff[ill_pos]==ill_move[u][y]){
++illcheck;
if(ill_pos==0)
ill_pos=100;
--ill_pos;
if(illcheck==ill_len[u]){
ill_done=u;
break;
}
}
else
break;
}
if(ill_done!=-1)
break;
}
if(ac==1||ill_done==1||ill_done==2||ill_done==6)
true_move=0;
if(bc>2)
true_move=1;
if(cc>2)
true_move=2;
if(ill_done==0||ill_done==3||ill_done==4)
true_move=4;
if(ill_done==5)
true_move=5;
return true_move;
}
and this is how i call the function:
int open_move;
open_move=which_move(acheck,bcheck,ccheck);
and open_move never match true_move.
I've tried to convert to something like this
int which_move(int *true_move,int ac,int bc,int cc)
and remove int true_move; and the return of return true_move; and implement the function like this:
int open_move;
which_move(open_move,acheck,bcheck,ccheck);
still i get it to work.
I've googled til chrome starts lagging because of too many tabs open and tried every trick I can find, but I'm not getting any wiser. Please help me with what I'm doing wrong.
Thanks from a hobbyist.

How to code Caesar Cipher encryption using file in C

For Caesar cipher encryption, I have this code. This program uses text written by the user. But I want this to be read from a text file and run.
#include<stdio.h>
#include <conio.h>
#include<string.h>
void main()
{
int key,i;
char [30];
clrscr();
printf("\n enter plain text : ");
gets(data);
printf("\ enter key value : ");
scanf("%d",&key);
{
for (i=o;i<strlen(data);i++) {
if (data[i]==' ') {}
else
{
if (data[i]>='x')
{
data[i]=data[i]-26;
}
data[i]=data[i]+key;
}
}
}
printf("your cipher text is : %s",data);
getch();
}
Did you just copy and paste this code from somewhere?
As everyone else has already pointed out, there are some pretty big problems with your code, so I'll try and touch on the ones they haven't mentioned throughout. To start though, you should declare main as an int. The return value tells you if the program exited correctly, it's not just convention.
Here is your original code, formatted. I removed conio.h because it's been deprecated for years and moreover you don't need it here. All it was doing was clearing the screen for you, which you can do with System("CLS");, although here is a great article that's been shared many times here on SO that explains why you should refrain from using it.
#include<stdio.h>
#include<string.h>
int main()
{
int key,i;
// original: char [30];
// I'm guess you copied and pasted wrong?
char plainText[30];
printf("\n enter plain text : ");
gets(data);
printf("\nenter key value : ");
scanf("%d",&key);
// You had brackets here, essentially creating an unnecessary
// block of code.
for(i=o;i<strlen(data);i++)
{
// Where did the "data" variable come from? You haven't declared it
// and you're trying to get its length in this for loop
if (data[i]==' ')
{
// ?
}
else
{
if (data[i]>='x')
{
// Instead of this approach I would go with a modulus operation
// If you're not familiar with modular arithmetic I really recommend you look
// it up. It's absolutely essential in cryptography. It's central to both
// symmetric and asymmetric key cryptography.
data[i]=data[i]-26;
}
// This is the heart of the Caesar cipher right here. This is where you're actually
// shifting the characters, so it's a literal Caesar cipher
data[i]=data[i]+key;
}
}
// Again, you haven't declared "data" so you can't call it. I'm guessing you didn't
// try to compile this program because it is teeming with errors GCC, Clang, and VC++
// would have caught immediately
printf("your cipher text is : %s",data);
getch();
// Remember to make your "main" function return an <code>int</code>. This value is
// very important, especially when your program gets called by another program because
// it's how your program communicates that it ran successfully, or something went
// wrong. In that case you could set a specific error code to know exactly
// what went wrong and were
// Example:
int x = 1;
if (x == 1)
exit(4);
// This program would now exit with a status of 4, which you would then know was due
// to this function. This was a contrived example, but hopefully you get the gist
return 0;
}

function prototype in c, compile error

So am trying to learn c by-myself (basically not having any previous experience in any programming language) and now I have some issues with prototyping some of my functions to use in header files.
For the sake of learning I only use the < stdio.h > lib and only use the printf and scanf functions and for now it only prints to console.
I was able to code a working prototype function for my menu that only uses the printf function but the scanf gives me more issues and it just refuses to compile and am having trouble to see where my thinking error is.
my main program:
#include "menu.h"
#include "circlefunctions.h"
#include "input.h"
int main(void){
float diameter;
double straal;
double oppervlakte;
double omtrek;
while(1){
menu();
user_input();
system("cls");
switch(user_input())
{
case 1:
printf(" ----------------------------------------\n");
printf(" Typ de diameter van de cirkel: ");
scanf("%g", &diameter);
printf(" ----------------------------------------\n");
straal = diameter / 2;
oppervlakte = PI * (straal * straal);
omtrek = 2 * PI * straal;
printf(" De straal = %f \n\n", straal );
printf(" De oppervlakte = %f \n\n" , oppervlakte);
printf(" De omtrek = %f \n" , omtrek);
printf(" ----------------------------------------\n");
break;
case 2:
return(0);
case 3:
return(0);
case 9:
return(0);
case 0:
return(0);
}
}
return 0;
}
and the stubborn header:
#include <stdio.h>
void user_input();
void user_input(){
scanf("%d", &user_input);
}
The error that I get while trying to compile is in input.h
the part with; scanf("%d", &user_input);
errorcode: format '%d' expects argument type of 'int ', but argument 2 has type 'void () ()'.
And I also got an error on the switch in the main program that the switch quantity is not an integer. I suspect that this error is related but am not sure. I still have to debug that part but if anyone is willing to point me to the right documentation i would much appreciate it.
And a second question that I have is also related to headers: I have < stdio.h > already included in "menu.h". Would I need to include it again in "input.h"?
(if i understand correctly how the preprocessor works i should not have to include it but I can't find anywhere where this is explained in simple terms unfortunately.)
Edit:
Thank you all for providing valuable information.
#zenith Thank you for your example. I hope you don't mind me asking some more.
I have replaced my code with yours in the "input.h" and it will compile and run now. However the behavior has changed. For some unclear reason i now have to input the choice twice before the program accepts my input. So the 1st input gets ignored after an enter and it will only accept the 2nd input.
Could you perhaps point me in the direction what causes this bug? or perhaps point me to some documentation where this is explained? I don't want to take up to much of you valuable time of-course.
Edit 2
Thanks for the reply and info. I got the bug out and it is working as intended(that was silly of me not to see that).
And to the rest who replied: Ill take your information of-course and also learn from that. Thank you all!
user_input() doesn't return anything, since it's declared void.
But you're trying to use the non-existing return value: switch(user_input()).
This causes undefined behavior.
Additionally, this:
scanf("%d", &user_input);
tries to read an int from stdin and store it in the memory address of the user_input function. Not a good idea. Again, undefined behavior.
What you probably want the function to look like:
int user_input(){
int number; // store user input to this variable
scanf("%d", &number);
return number; // return the user input so that it can be used outside the function
}
If you have header files declared in a previous header file. You will not need to include it again in the subsequent included header files. I tend to not include header files in my local *.h files just for that reason. It avoids circular includes if you declare your includes in the .c files as much as possible.
Your scanf function has as its second argument a function of type void(), void(). Meaning it takes no arguments and returns nothing or "void". I think you want your user_input to be a variable of type 'double' that is filled somewhere, maybe via some user input from the console using a call to 'gets' from stdin.
HTH

'return accept int, return int' contains no buildables

I am new to writing code and this is my first question on this site so please forgive my ignorance if there is an obvious solution for this.
When I compile and run the code in Xcode I get a message I have never seen before. I think it has more to do with the return statement but can't seem to fix it by changing the return statement. This is an example from my text book so I would think the logic is sound. I have tried running this code on more than one computer(all mac OSX) with the same results.I have tried different IDEs including codeRunner and Xcode 4.6.3 and 3.4.? and the latest release.
Can anyone tell me what the following message means? It seems not to be a compiling error but a message in a popup window.
THE MESSAGE
"The scheme 'return accept int, return int' contains no buildables that can be built for the SDKs supported by the run destination My Mac 64–bit. Make sure your targets all specify SDKs that are supported by this version of Xcode."
Thanks in advance.
#include <stdio.h>
#include <math.h>
int cubed(int var);
float main (){
int x;
printf("Please enter an integer \n");
scanf(" %d", &x);
printf(" %d ^ 3 = %d \n",x, cubed(x));
return 1;
}
float cubed(int var){ //This line was "int cubed(int var)" before I started playing with it.
float result;
result = pow(var, 3.0);
return(result);// I get the same message if I take out this line
}
I realized the issue with the return type conflict and have changed the code to:
#include <stdio.h>
#include <math.h>
int cubed(int var);
int main (){
int x;
printf("Please enter an integer \n");
scanf(" %d", &x);
printf(" %d ^ 3 = %d \n",x, cubed(x));
return 0;
}
int cubed(int var){
float result;
result = pow(var, 3.0);
return(result);
}
I now know that it is a setting issue within Xcode.
I have attached a series of screen shots illustrating the message I was receiving, what I did, and the new error I'm getting now. I was able to get the code to run in the IDE CodeRunner but not Xcode.
-- I guess I don't have the reputation points to post images. --
But now I get this new message.
" your mac runs an osx that is lower than the minimum require for your project.--Change your project's minimum deployment target or upgrade your version of OS X."
Now what?
I think I had changed the return type in the function header when troubleshooting early on but forgot about the function prototype, things get a bit confusing when I have to jumping from one editor to another to another trying to troubleshoot. My class uses Dev C++ and sometimes this causes me some confusion.
Can anyone tell me how I could fix this new issue or how to avoid it in the future?

Window instantly closing when opening compiled .exe in C

I have tried so many things. Running from command line, running from cmd, running with /K, putting system("pause"); getchar(); getch(); before return 0 and I simply can't get it to run. I'm writing in Notepad++, compiling in Cygwin and the window appears blank for the split second it appears (according to my screenshot, it could have been taken too early). Basically I've tried anything I could Google myself to. So I figured it must be something wrong with my code that the debugger doesn't show.
#include <stdio.h>
int main()
{
float lt1, lt2, dmg, x;
lt1=10;
lt2=30;
while(lt2>dmg)
{
while(x>0 || lt2>dmg)
{
dmg=dmg+x*lt1;
x--;
return (dmg);
}
x=x+0.01;
return (x);
}
printf("Horde factor is: %f", x);
return 0;
}
I would appreciate any help I can get, and I hope you will bear over with my inexperience.
You have undefined behavior in your code.
When you declare a local variable without assigning anything to it, its value is indeterminate. Usage of this variable will be undefined behavior until you assign a value to it.
In this case it's the dmg and x variables that causes this problem.
Its because of these statement :
return (dmg); //this ends the code execution .. because you have returned something from main()
x=x+0.01;
return (x); // even this one is wrong
you are exiting the code there and never getting to the printf ..
there should only be one return in main() .. and at the end.
More problems with your code:
you don't initialise dmg and x , but you use them as parameters for while loop
float lt1, lt2, dmg, x; // dmg,x uninitialized
In the outer while loop .. its an infinite loop as you don't do anything to the parameters of that loop to get out of it.
Like I said above .. there should be only 1 return in main()
Maybe instead of returning you should look into break; ( i don't know if thats what you want or not as I don't understand your code )

Resources