Jump to content

help me with this code at C

digitalMaster

guys im a new member at C.

First year in university.

i have an exersice.It says that i have to write a program which ask the user to give only >0 integers to fill an array.The user gives as many as numbers he wants.(this is the first part of my exercise but i cant finish it)

ANY ideas??

my code crashes -_-

 

#include <stdio.h>
#include <stdlib.h>
 
 
int main()
{
int i;
int A;
char exit;
char choice;
 
printf("when you finish press exit.If you press exit now you will leave the preogram");
 
 
scanf("%s", &choice);
 
i==0;
while (choice != exit)
printf("give the number.");
scanf("%d", &A);
 while (A<=0)
    {
      printf("The array must have >0 numbers only");
      printf("Give another number which is correct");
      scanf("%d", &A);
      i++;
      }
 
{
 
 
 
i++;
   
printf("If you finish press exit");
scanf("%d", &choice);
}
}
Link to comment
Share on other sites

Link to post
Share on other sites

C++ ,not C.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

First thing:

 

i==0;

 

That's generally used for comparison, not setting a value. You can actually do it in one line:

int i=0;

 

Also, A needs to be defined as an Array, not as int A - at the point where you define it, i has no value. 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

C++ ,not C.

it looks like C to me

 

reading "as many numbers as the user wants" is not that quick to do, are you sure you're not given an upper limit?

 

also, your program has some logic issues, i suggest writing a "working" pseudocode first, to make things clear in your head

especially the "reading the number until it's positive" part has some problems

 

when you have a clear idea of the logic you need to implement, we can jump into actual code, but first things first

Link to comment
Share on other sites

Link to post
Share on other sites

it looks like C to me

 

reading "as many numbers as the user wants" is not that quick to do, are you sure you're not given an upper limit?

 

also, your program has some logic issues, i suggest writing a "working" pseudocode first, to make things clear in your head

especially the "reading the number until it's positive" part has some problems

 

when you have a clear idea of the logic you need to implement, we can jump into actual code, but first things first

no i havent an upper limit

Link to comment
Share on other sites

Link to post
Share on other sites

i==0 is comparison, not assignment.

 

int A cannot be declared without assigning a positive value to i first. Otherwise that results in undefined behavior.

And an array must have a concrete size, you can't just say that it's size is 'i' and expect it to grow as you put more numbers or increment 'i'.

 

You can't scanf("%s", ...) into a char. You need a char array, either "char* choice = malloc(sizeof(char) * <size of string>) ;" or "char choice[<size of string>+1]; choice[<size of string>] = '\0';". I recommend the former to avoid security (among other) issues.

 

"while (choice != exit)" should be "while(strcmp(choice, "exit") != 0)". Also, you are missing an opening bracket there.

 

There is an extra i++ inside the "while(A<=0)" block.

And from there it is generally not finished. (A wild "if you finished press exit" when the user has already exited?! And some random brackets).

 

This is the code oriented review. But like @Ciccioo said, you need to perform some revisions at higher levels.

Link to comment
Share on other sites

Link to post
Share on other sites

i==0 is comparison, not assignment.

 

int A cannot be declared without assigning a positive value to i first. Otherwise that results in undefined behavior.

And an array must have a concrete size, you can't just say that it's size is 'i' and expect it to grow as you put more numbers or increment 'i'.

 

You can't scanf("%s", ...) into a char. You need a char array, either "char* choice = malloc(sizeof(char) * <size of string>) ;" or "char choice[<size of string>+1]; choice[<size of string>] = '\0';". I recommend the former to avoid security (among other) issues.

 

"while (choice != exit)" should be "while(strcmp(choice, "exit") != 0)". Also, you are missing an opening bracket there.

 

There is an extra i++ inside the "while(A<=0)" block.

And from there it is generally not finished. (A wild "if you finished press exit" when the user has already exited?! And some random brackets).

 

This is the code oriented review. But like @Ciccioo said, you need to perform some revisions at higher levels.

 

 

no i havent an upper limit

 

 

it looks like C to me

 

reading "as many numbers as the user wants" is not that quick to do, are you sure you're not given an upper limit?

 

also, your program has some logic issues, i suggest writing a "working" pseudocode first, to make things clear in your head

especially the "reading the number until it's positive" part has some problems

 

when you have a clear idea of the logic you need to implement, we can jump into actual code, but first things first

 

 

First thing:

 

i==0;

 

That's generally used for comparison, not setting a value. You can actually do it in one line:

int i=0;

 

Also, A needs to be defined as an Array, not as int A - at the point where you define it, i has no value. 

 

 

C++ ,not C.

Could someone make a code for me?

Link to comment
Share on other sites

Link to post
Share on other sites

Could someone make a code for me?

no, sorry

 

you do your code

Link to comment
Share on other sites

Link to post
Share on other sites

As others have pointed out, you have quite a bit of errors in your original code sample. 

 

The array's capacity needs to be determined at compilation time.  This means that you cannot just give it a value "i" and expect it to figure out the size of the array on its own.  You need to give it a constant like 5 or 10.  There are ways to dynamically bind an array, but I'm going to assume you're not at that level yet...so let's just stick with static binding. 

 

If you want an array to hold, say...8 integers, you can decalre it like so: 

 

int arrayName[8];

 

Now remember, this creates an array that holds 8 elements (0-7) with each element being an integer value.  To assign an integer to an element, you would do something like this: 

 

arrayName[0] = 20;  // this sets the first element in the array, element 0, to hold the value of 20

 

You would need to do this for each value the user enters while the program is running.  It would be in your best interest to use loops to efficiently assign values to each element in the array, but I don't know if you've gotten that far yet.  The description of your assignment is awfully vague, so I don't know what your teacher wants you to show proficiency in. 

 

Also, don't expect anyone to just write a bunch of code for you.  We're here to help with questions you may have and to give you some helpful hints, but we're not here to do your homework.  I've already given you a couple of lines of code and explained what each line does; try and use that to help you with the assignment.  If you're still unsure about something, feel free to ask.  However, no one (including myself) is going to do your homework for you. 

Link to comment
Share on other sites

Link to post
Share on other sites

Just realized you've stated in your OP that this was just the first part. So I guess if it'll help since I can't help write the code for you but I can try to break it down for you.

 

Step 1.

  • Declare a variable which will be used as a reference point to your array index.
  • Declare your prompt statements, so ex. say and prompt everything you want to tell the user such as errors at input or try again statements or exit statements etc.

Step 2.

  • Ask for input, call the prompts asking for input from the user and do a test sample.
  • If you were able to get a couple values in repeat the process and get a feel for it before you put the body of that input into a loop statement
  • while(characters into array != EXIT || character into array < 0) {Ask for input - > insert into array -> increment variable used as reference to index of array}

Step 3.

  • Come up with your exit procedure inside while loop as you've already attempted.
  • ex.
    • if(char input == exit)
      • {char = -1//next loop should exit if it didn't to begin with}

Step 4.

  • Keep track of your values
  • At the end of the program before exiting output your variables used, at least then you can keep track of everyything. If you're using visual studios you can set breakpoints by clicking on the side of the line statements outside the body of the code, then just f10 and f11 your way in and out of every statement and you can also right click variables to keep watch on them to see if their values are changed appropriately or not.

Too high to think now, and too tired to follow up anymore. Sorry I didn't provide any code in C for your syntax issues but just keep comparing your code with those found online and you should be able to trial and error your way through this for now. Eventually you'll look back and give yourself a facepalm. Old code does that to you.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×