C LANGUAGE >NOOB HERE< what did i do wrong? - c

for(a=0;a<99;a++){
reg:
system("cls");
printf("\t\t\t~~REGISTER AN ACCOUNT~~\n\n");
printf("\tDesired Username: ");
scanf("%s", &user[a].user);
for(b=0;b<=a-1;b++){
if(strcmp(user[a].user,user[b].user)==0){
printf("USERNAME IS ALREADY TAKEN");
system("pause");
goto reg;
}
}
printf("\tDesired Password: ");
scanf("%s", &user[a].pass);
printf("\tPersonal/Company Name: ");
scanf("%s", &user[a].name);
printf("\tAddress/Location: ");
scanf("%s", &user[a].address);
printf("\tEmail-Address: ");
scanf("%s", &user[a].email);
printf("\tContact Number: 09");
scanf("%d", &user[a].contact);
break;
}
whenever I put the break in the end the system doesn't recognise if the username is taken or not, but when 'break' is removed it can recognise it.. idk how is this happening.. please help..
PS. sorry if this looks or sounds really stupid to you I am just starting to learn to program(I m trying to make a register program with a very limited knowledge for a school project)

I think the problem lies in your for loops:
First iteration:
a = 0;
for(b=0;b<=a-1;b++)
will not be true
as b<=a-1 equates to b<=-1 which is not true. Hence it will not enter the for loop for b. It will go to break and exit
Same process will repeat until a>=2,
When you don't have break , b waits for b>=2 and starts comparing. That's why you see result of comparison.
A simple check would be to print out values of a and b at every iteration.

Related

Why both of if statements are executed no matter what the input is?

In my choose your own adventure, A and B are both inputs that result opening input functions. Whichever is put down, they are both displayed.
#include <stdio.h>
int main()
{
char firstname[15];
char class;
char swordch0c1;
char enter;
printf("Hello there! Could I have your first name?\n");
scanf("%s",firstname);
printf("\n---------------------The Legend of %s---------------------",firstname);
printf("\nPress Enter to continue.");
enter=getch();
if(enter=='\n');
printf("\n\n\nYou are %s, a: \nA.Swordsman\nB.Assassin\nC.Archer\nD.Mage\n",firstname);
scanf("%c", &class);
/*swordsman story starts here*/
if(class=="A");
{
printf("\n\nThere you stand, at your boring everyday post.\nWhen you joined the army, you thought it would be more exciting than this.\nJust then, you see your general walking towards you.");
printf("\n\nYou quickly improve your posture. \"Soldier, I have an opurtunity for you\"\nA.\"Really? What is it?\"\nB.\"I'm not interested\"\n");
scanf("%d",swordch0c1);
if(swordch0c1=="b");
{
printf("\"But... I didn't even tell you what it was. Okay, suit yourself\" You are DOOMED to a life of boredom.\n\n\n\n\n");
}
if(swordch0c1=="a");
{
printf("\n\n\n\"Well, you see, there's this dragon. He's been causing big problems. \nHe's destroyed villages, harrassed the priests on the mountain,\n");
printf("and even attacked a couple cities. His name is Sorrith, and dozens of knights have already tried to kill him, none of them being successful.\"");
printf("\nA.\"Say no more, I'll do it.\"\nB.\"Dragon? No way! You better find someone else.\nC.\"Keep talking...\"");
}
}
return 0;
}
I apologize if I'm not specific/clear enough.
I'm using Windows BTW.
You made a few mistake, but this should work!
#include <stdio.h>
int main() {
char firstname[15], character, swordch0c1, enter;
printf("Hello there! Could I have your first name?\n>");
scanf("%s", &firstname);
printf("\n---------------------The Legend of %s---------------------", firstname);
printf("\nPress Enter to continue.");
enter=getch();
printf("\n\n\nYou are %s, a: \nA.Swordsman\nB.Assassin\nC.Archer\nD.Mage\n>", firstname);
scanf(" %c", &character);
/*swordsman story starts here*/
if (character == 'A') {
printf("\n\nThere you stand, at your boring everyday post.\nWhen you joined the army, you thought it would be more exciting than this.\nJust then, you see your general walking towards you.");
printf("\n\nYou quickly improve your posture. \"Soldier, I have an opurtunity for you\"\nA.\"Really? What is it?\"\nB.\"I'm not interested\"\n>");
scanf(" %c", &swordch0c1);
if (swordch0c1 == 'b')
printf("\"But... I didn't even tell you what it was. Okay, suit yourself\" You are DOOMED to a life of boredom.\n\n\n\n\n");
if (swordch0c1 == 'a') {
printf("\n\n\n\"Well, you see, there's this dragon. He's been causing big problems. \nHe's destroyed villages, harrassed the priests on the mountain,\n");
printf("and even attacked a couple cities. His name is Sorrith, and dozens of knights have already tried to kill him, none of them being successful.\"");
printf("\nA.\"Say no more, I'll do it.\"\nB.\"Dragon? No way! You better find someone else.\nC.\"Keep talking...\"");
}
}
return 0;
}
Remove all the semicolons after each if and also use ' instead of " in all the if(...) . Also,
scanf("%d",swordch0c1);
Is wrong as swordch0c1 is not an int. Use %c instead of %d as it is a char that you are scanning
You put semicolons after your both if...
Change if(swordch0c1=="b"); by if(swordch0c1=='b') and if(swordch0c1=="a"); by if(swordch0c1=='a')
I think you should read again your C tutorial or your C book, chapter instruction.
Don't put semicolon after if statement, otherwise it would skipped. Always use single quote for a char, and double for string.

Simple Multichoice, Multivariable, Calculator Query

I recently programmed this code (C) for a pretty simple calculator in Xcode.
It works mostly but does not display the answer for the sum/s.
The code is as follows:
#include <stdio.h>
int main()
{
//int's & chars-----------------
char SumMethod;
int firstnumber, secondnumber;
//int's & chars------------------
puts("Calculator v0.6");
printf("Please input first number: "); //Prompts 'firstnumber' input
scanf("%d", &firstnumber); //Scan's 'firstnumber' input and saves to '&firstnumber'
printf("Please input second number: "); //Prompts 'secondnumber' input
scanf("%d", &secondnumber); //Scan's 'secondnumber' input and saves to '&secondnumber
printf("d" "Please select Method: +(a), -(b), *(c), /(d): ");
scanf("%c", &SumMethod); //Scan's 'SumMethod' input
if(SumMethod=='a') {
printf ("%d",firstnumber + secondnumber); //This section detects the SumMethod and outputs the corrisponding sum
}
else if (SumMethod=='b'){
printf("%d",firstnumber-secondnumber);
}
else if (SumMethod=='c') {
printf("%d",firstnumber * secondnumber);
}
else if (SumMethod=='d') {
printf("%d",firstnumber / secondnumber);
}
}
For the solution i'm looking for a response that doesn't drastically change the code as how it is now is what I understand and because i'm relatively new to coding in general however if there is an obvious fix using useful common tools that would be much appreciated.
Thanks, Xenon
Your problem is that your comparisons are wrong. You need to compare your input to the LETTER a and not the (undefined and essentially random) values of the variables a,b,c,d.
so it is
if(SumMethod=='a')
instead of
if(SumMethod==a)
You don't need your variables a,b,c,d at all.

C program stops in the middle of running

Hi im new here also im new to programming and id like you to help me on this : to problem is that after compiling and running the program it stops in the middle of it when running and i didnt know what is causing this and sorry for the unreadable previous post :
here is my program :
char answer[15];
char place[15];
char fullname[15];
int age;
printf("What Is Your Full Name?: ");
scanf("%s",fullname);
printf("What Is Your Age?: ");
scanf("%d",age);
printf("Where Do You Live?: ");
scanf("%s",place);
if(strcmp(place,"gafsa")==0) {
printf("Aint a bad place you know");
}
else{
printf("hmmm %s cool\n",place);
}
printf("your name is %s, %d year old from %s is that right?: ",fullname,age,place);
scanf("%s",answer);
if(strcmp(answer,"yes")==0){
printf("you my friend are awesome\n");
}
else{
printf("you suck\n");
}
and this is an image to show the problem clearly:
http://i.stack.imgur.com/yFTwK.png
You need to pass the address of the variable:
scanf("%d",&age);
^
You're taking input at a memory location of value of uninitialized age. i.e. some garbage
Use:
scanf("%d",&age); // notice & , pass address of variable age

program running through my if else after function call

I have a class assignment in C to make a simple calculator that performs three calculations. I haven't completed all of the functions yet but I am having a problem with my calcMenu function. When the function is called the program runs through all of the if else statements and unknown to me, performs only the else statement which is error checking. Than the function is run again which is intended but this time it does not run through all of the if else statements and allows the user to make a choice. I know I have done something really stupid but have been racking my brain for the last hour. If anyone has any pitty for me, than please point me in the right direction. I know all the system calls will Irk some but this is a basic class and our instructor has told us to use them.
Thanks in advance,
Mike
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define pause system ("pause")
#define cls system ("cls")
//Prototype calculate functions here
void evenOrOdd(int userNumber);
void squareNum(int userNumber);
void cubeNum(int userNumber);
void calcMenu(int userNumber);
void main() {
//Declare local variables here
int userNumber = 0;
printf("\t\t\tThe amazing three function caluculator\n\n\n");
printf("Please enter a whole number that you would like to calculate\n");
scanf("%d", &userNumber);
calcMenu(userNumber);
}
void calcMenu(int userNumber)
{
char calculateOption;
printf("\nWhat calculation would you like to perform with your number?\n\n");
printf("Press A.) to check if your number is even or odd.\n\n");
printf("Press B.) to calculate the square of your number.\n\n");
printf("Press C.) to calculate the cube of your number.\n\n");
printf("press D.) to exit the program.\n");
scanf("%c", &calculateOption);
calculateOption = toupper (calculateOption);
if (calculateOption == 'A')
{
evenOrOdd(userNumber);
}
else if (calculateOption == 'B')
{
squareNum(userNumber);
}
else if (calculateOption == 'C')
{
cubeNum(userNumber);
}
else if (calculateOption == 'D')
{
system("cls");
printf("Thank You for using my amazing calculator.\n\n");
system ("pause");
system ("exit");
}
else
{
printf("Please enter a valid choice");
calcMenu(userNumber);
}
}
void evenOrOdd(int userNumber) {
userNumber = userNumber %2;
if (userNumber == 0)
{
printf("Your number is even. \n");
}
else
{
printf("Your number is odd. \n");
}
}
void squareNum(int userNumber) {
}
void cubeNum(int userNumber){
}
When you read input with scanf you have to press the Enter key to make the program continue. Your scanf call reads the single character from the input, but leaves the Enter key still in the input buffer, to be read next time you call scanf.
There is a very simple trick to solve that: Place a space in the scanf format string before or after the "%c". This will make scanf skip whitespace.
scanf("%c ", &calculateOption);
If you stepped through the code with a debugger you would have easily seen that calculateOption would have been the newline character.
First of all, You can condense all those printf statements into one function to save the extra calls.
Next, you should probably indent your functions, I can't tell where one begins and another ends at a glance.
Third, don't use system("pause"), use getchar().
Fourth, this is optional, you might want to turn those if statements into a switch statement.
Now, on to your question. First of all, instead of using scanf("%c", &calculateOption), just use getchar() here too. In this case, I would write calcMenu() as this:
int calcMenu(int userNumber){
printf("\nWhat calculation would you like to perform with your number?\n\n\
Press A.) to check if your number is even or odd.\n\n\
Press B.) to calculate the square of your number.\n\n\
Press C.) to calculate the cube of your number.\n\n\
Press D.) to exit the program.\n");
switch(toupper(getchar())){
case 'A':
evenOrOdd(userNumber);
break;
case 'B':
squareNum(userNumber);
break;
case 'C':
cubeNum(userNumber);
break;
case 'D':
system("cls"); //this is bad, really.
printf("Thank You for using my amazing calculator.\n\n");
getchar();
return 0;
default:
printf("Please enter a valid choice: ");
calcMenu(userNumber);
break;
}
}
Also, main should always return a value. void main is bad practice.
Disclaimer: The code isn't tested, you shouldn't copy/paste it anyways. I also don't know if you're being forced to use some things or not...

How to sort blank arrays to the top so I can delete them

int id_swap()
{
char tempstring[15];
strcpy(tempstring, id[index+1]);
strcpy(id[index+1], id[index]);
strcpy(id[index], tempstring);
}
int delete_student()
{
int found=0;
char id_to_find[10];
printf("Please enter the student ID to delete\n\n");
scanf("%s", & id_to_find);
fflush(stdin);
system("cls");
for(index=0;index<height_of_array+1;index++)
{
if(strcmpi(id_to_find, id[index]) == 0)
{
found=1;
id_swap();
system("cls");
printf("Student deleted");
height_of_array = height_of_array--;
}
Okay so that's part of my code. A quick example of what happens
I sort the students that are already in the program so it comes up like this for e.g
Student#1 Chris ID: 1831
Student#2 etc
student#3 etc
student#4 Brian ID: 4432
student#5 etc
student#6 etc
But when I try to delete say Brian for example, it deletes it but then it looks like this
student#1 ID:
Student#2 Chris ID:1831
Student#3 etc
Is there any way to move that blank array to the last position so I can then decrement my "Height_of_array" so that the number of storable students goes down by 1 to reflect the deletion
There are a few problems with your code: that jump at me:
fflush(stdin);
Never do that. Only flush output streams.
height_of_array = height_of_array--;
Is undefined behavior, you meant:
height_of_array--;

Resources