Jump to content

simple problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello I am writing a simple array to get into C++ but got stuck, if you can let me know what am I doing wrong I would appreciate it.

Thank you



#include <iostream>

using namespace std;


//main function

int main ()

{

	int num1 = 0, num2 = 0, num3 = 0, num4 = 0;

	int result = 0, n = 0;

	int numlist [] = {num1, num2, num3, num4};

	cout << "please type in 4 numbers\n";

	cin >> num1;

	cin >>  num2;

	cin >> num3;

	cin >> num4;

	cout << "you have entered: "<< numlist << "numbers";

	while(n < numlist)

	{

		numlist[n];

		n++;

	}

		

return 0;

}



#2
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts

	cout << "you have entered: "<< numlist << "numbers";

	while(n < numlist)

	// ...


numlist returns pointer to first element in your array.
Try this :


	cout << "you have entered: "<< sizeof(numlist) / sizeof(int)<< "numbers";

	while(n < sizeof(numlist) / sizeof(int) )

	// ...

sizeof returns size in bytes, so you need to get sizeof array and divide it with sizeof single element.

#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello julmuri,

Thank you for the info, I wish it was as easy as C#, you just need a .length etc, but oh well, anyways two other questions I might ask is, how do you tell it to stay at the on the command prompt until the user hits any key, for C# its Console.ReadKey(); but what is it for C++? also how do you tell it in C++ to end the command prompt in a few seconds after its done running the program and it will close without user hitting the key to close the program?

Thank you

#4
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts

Siten0308 said:

Hello julmuri,

Thank you for the info, I wish it was as easy as C#, you just need a .length etc, but oh well, anyways two other questions I might ask is, how do you tell it to stay at the on the command prompt until the user hits any key, for C# its Console.ReadKey(); but what is it for C++?
Hmm I think std::cin.ignore() will do the job.

Siten0308 said:

also how do you tell it in C++ to end the command prompt in a few seconds after its done running the program and it will close without user hitting the key to close the program?
You want some sort of sleep function ?
You could use native api, Sleep on windows ( windows.h ) or sleep ( unistd.h ) on nix.
Not sure if there is more elegant solution to this. (:

#5
FakeRobotGymnast

FakeRobotGymnast

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
Simple problem has simple solution. Remove all intermediate variables. I assume you've used java before C++? C++ does pass by value by default.
By the way, what is this program even supposed to be doing? I removed all pointless code... but the entire thing is pointless.
Fix below:


#include <iostream>

using namespace std;


//main function

int main ()

{

	int result = 0, n = 0;

	int numlist [] = {0};

	cout << "please type 4 numbers\n";

	cin >> numlist[0];

	cin >>  numlist[1];

	cin >> numlist[2];

	cin >> numlist[3];

	cout << "you have entered 4 numbers";

return 0;

}


#6
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
getchar();
fflush(stdin);

fflush flushes the buffer, getchar halts it from exiting when you press enter. Dunno if C++ has the same functions, but I would assume so.

Theres also system(pause); that I know of, but dunno if it does the same or not, tbh.
Posted Image