Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need to make a C# program that asks the user for 5 numbers and prints them in reverse, I kinda used an array but it needs to work with a Stack, this is the code and the error will be highlighted

public class Stack_01
{
    public static void Main()
    {
        int[] number = new int [5];

        Stack miPila = new Stack();
        for (int i = 0; i < numero.Length; i++)
        {
            Console.WriteLine("Type a number{0}:", i+1);
            number[i] = int.Parse(Console.ReadLine());

        }

        Console.WriteLine();
        Array.Reverse(number);
        Console.WriteLine("these are your numbers:\n");
        for (int i = 0; i < numero.Length; i++)
        {
            numero = (int)miPila.Pop();
            Console.WriteLine(number[i]);
        }

   
       
    }

}


What I have tried:

Well bassically tried to add an convert to int 32 i saw on StackOverun that it could've worked but it didn't.
Posted
Updated 28-Jul-20 16:40pm
Comments
Nelek 28-Jul-20 16:31pm    
A couple of things that come at first glance...
You declare "number" but then use "numero.length" in the loop.
You declare the Stack "miPila" but never "push" anything inside it before using the "pop"
If you enter a letter and hit enter your program will not work properly, because you assign the output of int.parse directly, instead of checking first if it is a number.
josmer benjamin suero sencion 28-Jul-20 19:57pm    
I tried to translate it to English so it could be better understood, I forgot that one
josmer benjamin suero sencion 28-Jul-20 19:59pm    
Sorry for replying now, didnt see the comment.
josmer benjamin suero sencion 28-Jul-20 19:59pm    
how could i push the answer the user gives me into "miPila" so i can show it later?
josmer benjamin suero sencion 28-Jul-20 20:57pm    
I tried to fix it, but now it won't let me add the values:

public class Stack_01
{
public static void Main()
{
//int[] numero = new int[5];

Stack numero = new Stack(5);

for (int i = 0; i < numero.Count; i++)
{
Console.WriteLine("Ingrese un numero{0}:", i + 1);
numero.Push(5);

}

//Console.WriteLine();
// Array.Reverse(numero);
Console.WriteLine("Estos son tus numeros:\n");
for (int i = 0; i < numero.Count; i--)
{
Console.WriteLine(numero.Pop());
}

//for (int i = 0; i < miPila.Count; i++)
//{
// numero = (int)miPila.Pop();
// Console.WriteLine(numero);
//}

}

}

1 solution

You are using the general purpose System.Collections.Stack which holds objects. Use the generic System.Collections.Generic.Stack<T> [^]:
C#
Stack<int> yourStack = new Stack<int>(5);
Because Stack is enumerable (Array like), you can read the values using indexes, and iterate without removing them using a 'for or 'foreach loop.

Suggestion: use Int32.TryParse to make sure the user entered an Int: [^]
 
Share this answer
 
Comments
josmer benjamin suero sencion 28-Jul-20 23:13pm    
I had an updated version of the code i posted earlied and added your suggestion, but it only shows numbers from 4 to 0, its like it isnt saving the numbers on the stack to show them later, this is the code:
using System;
using System.Collections;
using System.Collections.Generic;

public class Ejemplo_11_02a
{
public static void Main()
{


Stack<int> yourStack = new Stack<int>(5);

for (int i = 0; i < 5 ; i++)
{
Console.WriteLine("Ingrese un numero {0}:", i + 1);
yourStack.Push(i); Int32.TryParse(Console.ReadLine();
}


Console.WriteLine("Estos son tus numeros:\n");
for (int i = 0; i < yourStack.Count; i--)
{
//Console.WriteLine("Estos son tus numeros:\n");
// Console.WriteLine(numero.Pop());
Console.WriteLine(yourStack.Pop());


}


Console.ReadKey();
}

}
BillWoodruff 28-Jul-20 23:29pm    
In this code you are not using Int32.TryParse correctly. If you push values 0~4 on the Stack, you have five values.

Put breakpoints in your code, and single step through it: observe the contents of the Stack carefully, and study the links I posted fo you.

What is important is you learning how to debug.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900